File tree 1 file changed +13
-6
lines changed
apps/react-world/src/apis
1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change 1
1
import type { AxiosRequestConfig } from 'axios' ;
2
2
import axios from 'axios' ;
3
+ import Cookies from 'js-cookie' ;
3
4
4
5
// TODO: 향후 .env 파일로 분리
5
6
const BASE_URL = 'https://api.realworld.io/api' ;
@@ -12,14 +13,20 @@ const axiosApi = (url: string, options?: AxiosRequestConfig) => {
12
13
13
14
// POST, DELETE 등 요청 시 인증이 필요한 경우
14
15
const axiosAuthApi = ( url : string , options ?: AxiosRequestConfig ) => {
15
- const jwtToken = '토큰 값' ; // TODO: 향후 개발
16
- const instance = axios . create ( {
17
- baseURL : url ,
18
- headers : { Authorization : 'Bearer ' + jwtToken } ,
19
- ...options ,
20
- } ) ;
16
+ const instance = axios . create ( { baseURL : url , ...options } ) ;
21
17
return instance ;
22
18
} ;
23
19
24
20
export const api = axiosApi ( BASE_URL ) ;
25
21
export const authApi = axiosAuthApi ( BASE_URL ) ;
22
+
23
+ // 요청 전 JWT 쿠키 유무를 확인 후 Bearer 헤더 설정
24
+ authApi . interceptors . request . use ( config => {
25
+ const jwtToken = Cookies . get ( 'jwtToken' ) ;
26
+
27
+ if ( jwtToken ) {
28
+ config . headers . Authorization = 'Bearer ' + jwtToken ; // 토큰이 있을 때 헤더에 추가
29
+ }
30
+
31
+ return config ;
32
+ } ) ;
You can’t perform that action at this time.
0 commit comments