1- import axios from 'axios' ;
1+ import axios from "axios" ;
2+ import { useUserStore } from "@/stores/user" ;
23
3- const API_BASE_URL = import . meta. env . VITE_API_BASE_URL || 'http://localhost:8080' ;
4+ const API_BASE_URL =
5+ import . meta. env . VITE_API_BASE_URL || "http://localhost:8080" ;
46
57const api = axios . create ( {
68 baseURL : API_BASE_URL ,
79 withCredentials : true ,
810 headers : {
9- ' Content-Type' : ' application/json' ,
10- }
11+ " Content-Type" : " application/json" ,
12+ } ,
1113} ) ;
1214
1315api . interceptors . request . use (
1416 ( config ) => {
15- const token = localStorage . getItem ( ' accessToken' ) ;
17+ const token = localStorage . getItem ( " accessToken" ) ;
1618
1719 if ( token ) {
1820 // 토큰에서 공백 제거
@@ -25,7 +27,7 @@ api.interceptors.request.use(
2527 ( error ) => {
2628 return Promise . reject ( error ) ;
2729 }
28- )
30+ ) ;
2931
3032api . interceptors . response . use (
3133 ( response ) => {
@@ -36,24 +38,36 @@ api.interceptors.response.use(
3638 const { status } = error . response ;
3739
3840 if ( status === 401 ) {
39- localStorage . removeItem ( 'accessToken' ) ;
41+ if ( window . location . pathname === "/session-expired" ) {
42+ return Promise . reject ( error ) ;
43+ }
44+
45+ localStorage . removeItem ( "accessToken" ) ;
46+ const userStore = useUserStore ( ) ;
47+
48+ if ( userStore . isSessionHandling ) {
49+ return Promise . reject ( error ) ;
50+ }
4051
4152 const currentPath = window . location . pathname ;
4253
4354 // 로그인 페이지가 아닌 경우에만 리다이렉트
44- if ( ! currentPath . startsWith ( '/login' ) && ! currentPath . includes ( '/delivery/login' ) ) {
55+ if (
56+ ! currentPath . startsWith ( "/login" ) &&
57+ ! currentPath . includes ( "/delivery/login" )
58+ ) {
4559 // 배송 앱에서는 배송 로그인으로, 일반 앱에서는 일반 로그인으로
46- if ( currentPath . startsWith ( ' /delivery/' ) ) {
47- window . location . href = ' /delivery/login' ;
60+ if ( currentPath . startsWith ( " /delivery/" ) ) {
61+ window . location . href = " /delivery/login" ;
4862 } else {
49- window . location . href = ' /login' ;
63+ window . location . href = " /login" ;
5064 }
5165 }
5266 }
5367 }
5468
5569 return Promise . reject ( error ) ;
5670 }
57- )
71+ ) ;
5872
59- export default api ;
73+ export default api ;
0 commit comments