@@ -27,7 +27,7 @@ instance.interceptors.request.use(
2727 } ,
2828) ;
2929
30- let isAlertShown = false ;
30+ let tokenRefreshPromise : Promise < void > | null = null ;
3131let isLoginAlertShown = false ;
3232
3333// 응답 인터셉터
@@ -49,28 +49,20 @@ instance.interceptors.response.use(
4949 if ( error . response ?. data . code === ERROR_CODE . NO_ACCESS_TOKEN ) {
5050 if ( error . config ?. url === API_URL . USER . LOGOUT ) {
5151 safeLocalStorage . remove ( IS_LOGINED ) ;
52-
53- return ;
5452 }
5553
5654 redirectToLoginOnce ( ) ;
57-
58- return ;
5955 }
6056
6157 if ( error . response ?. data . code === ERROR_CODE . LOGIN_REQUIRED ) {
6258 // 인증 페이지에서는 로그인 페이지로 이동하지 않음
6359 if ( window . location . pathname !== "/auth" ) {
6460 redirectToLoginOnce ( ) ;
65-
66- return ;
6761 }
6862 }
6963
7064 if ( error . response ?. data . code === ERROR_CODE . REFRESH_TOKEN_EXPIRED ) {
7165 redirectToLoginOnce ( ) ;
72-
73- return ;
7466 }
7567
7668 if ( error . response ?. data . code === ERROR_CODE . PAYMENT ) {
@@ -80,22 +72,22 @@ instance.interceptors.response.use(
8072 // 액세스 토큰 만료
8173 if ( error . response ?. data . code === ERROR_CODE . ACCESS_TOKEN_EXPIRED ) {
8274 try {
83- if ( isAlertShown ) {
84- // 원래 요청 재시도
85- const originalRequest = error . config ;
86-
87- if ( ! originalRequest ) {
88- return Promise . reject ( error ) ;
89- }
90-
91- return instance ( originalRequest ) ;
75+ // 이미 토큰 재발급이 진행 중이면 완료될 때까지 대기
76+ if ( tokenRefreshPromise ) {
77+ await tokenRefreshPromise ;
78+ } else {
79+ // 토큰 재발급 시작
80+ refreshAccessToken ( )
81+ . then ( ( ) => {
82+ tokenRefreshPromise = null ;
83+ } )
84+ . catch ( ( error ) => {
85+ tokenRefreshPromise = null ;
86+
87+ throw error ;
88+ } ) ;
9289 }
9390
94- // 엑세스 토큰 재발급
95- isAlertShown = true ;
96- await refreshAccessToken ( ) ;
97- isAlertShown = false ;
98-
9991 // 원래 요청 재시도
10092 const originalRequest = error . config ;
10193
@@ -104,17 +96,11 @@ instance.interceptors.response.use(
10496 }
10597
10698 return instance ( originalRequest ) ;
107- } catch ( error ) {
99+ } catch {
108100 if ( typeof window !== "undefined" ) {
109- alert ( "로그인이 필요한 페이지입니다." ) ;
110-
111- // 엑세스 토큰 재발급 실패시 로그인 페이지로 이동
112- safeLocalStorage . remove ( IS_LOGINED ) ;
113-
114- window . location . href = "/login" ;
115- isAlertShown = false ;
101+ redirectToLoginOnce ( ) ;
116102
117- return ;
103+ return Promise . reject ( error ?. response ?. data ) ;
118104 }
119105 }
120106 }
0 commit comments