Skip to content

Commit bb33ef0

Browse files
committed
feat : add basic JWT auth
1 parent d946936 commit bb33ef0

File tree

7 files changed

+109
-10
lines changed

7 files changed

+109
-10
lines changed

jsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"jsx": "react-jsx", // React JSX 구문을 사용하도록 설정 (React 17+에서는 자동으로 JSX를 지원)
4-
"checkJs": true, // JavaScript 파일에 대한 타입 검사 활성화 (VSCode에서 오류 경고 및 코드 완성 기능 향상)
4+
"checkJs": false, // JavaScript 파일에 대한 타입 검사 (VSCode에서 오류 경고 및 코드 완성 기능 향상)
55
"baseUrl": ".", // 기본 경로를 현재 프로젝트의 루트로 설정 (상대 경로 대신 별칭 사용 가능)
66
"paths": {
77
"@/*": ["src/*"] // '@' 별칭을 'src' 폴더로 설정 (모듈 경로 참조 시 간단하게 사용 가능)

package-lock.json

+53-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"js-cookie": "^3.0.5",
1515
"postcss-cli": "^11.0.0",
1616
"react": "^18.3.1",
17+
"react-cookie": "^7.2.2",
1718
"react-dom": "^18.3.1",
1819
"react-router-dom": "^7.1.1",
1920
"zustand": "^5.0.3"

src/apis/AuthAPI.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
import axios from 'axios';
2-
3-
axios.defaults.baseURL = '';
4-
axios.defaults.withCredentials = true;
5-
6-

src/apis/AuthInstance.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import axios from 'axios';
2+
3+
const authAxiosInstance = axios.create({
4+
baseURL: '',
5+
timeout: 5000,
6+
headers: {
7+
'Content-Type': 'application/json',
8+
},
9+
withCredentials: true, //To use HttpOnly Cookie
10+
});
11+
12+
authAxiosInstance.interceptors.request.use(
13+
(config) => {
14+
console.log('Requset Interceptors Success : ', config);
15+
16+
const accessToken = localStorage.getItem('accessToken');
17+
if (accessToken) {
18+
config.headers.Authorization = `Bearer ${accessToken}`;
19+
}
20+
21+
return config;
22+
},
23+
24+
(error) => {
25+
console.log('Requset Interceptors Success : ', error);
26+
return Promise.reject(error);
27+
}
28+
);
29+
30+
authAxiosInstance.interceptors.response.use(
31+
(res) => {
32+
console.log(res);
33+
return res;
34+
},
35+
36+
async (error) => {
37+
console.log(error);
38+
const originalRequest = error.config;
39+
40+
if (error.response.status == 401 && !originalRequest._retry) {
41+
originalRequest._retry = true;
42+
}
43+
try {
44+
await axios.post('', {}, { withCredentials: true });
45+
return authAxiosInstance(originalRequest);
46+
} catch (refreshError) {
47+
console.log('Refresh Token invalid: ', refreshError);
48+
}
49+
return Promise.reject(error);
50+
}
51+
);
52+
53+
export default authAxiosInstance;

src/routes/router.jsx

Whitespace-only changes.

src/store/useStore.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { create } from 'zustand';

0 commit comments

Comments
 (0)