Skip to content

Commit ddffb4b

Browse files
committed
feat(app): 인증이 필요한 요청 시 interceptors 를 통해 JWT 쿠키가 있다면 Bearer 헤더를 설정하도록 함
1 parent 53a51bd commit ddffb4b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed
+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { AxiosRequestConfig } from 'axios';
22
import axios from 'axios';
3+
import Cookies from 'js-cookie';
34

45
// TODO: 향후 .env 파일로 분리
56
const BASE_URL = 'https://api.realworld.io/api';
@@ -12,14 +13,20 @@ const axiosApi = (url: string, options?: AxiosRequestConfig) => {
1213

1314
// POST, DELETE 등 요청 시 인증이 필요한 경우
1415
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 });
2117
return instance;
2218
};
2319

2420
export const api = axiosApi(BASE_URL);
2521
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+
});

0 commit comments

Comments
 (0)