Skip to content

Commit

Permalink
fix: refresh 요청 API 명세에 맞게 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Creative-Lee committed Nov 13, 2023
1 parent a4a83df commit 4f858e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions frontend/src/features/auth/remotes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { client, clientBasic } from '@/shared/remotes/axios';

interface AccessTokenRes {
accessToken: string;
}
Expand Down
16 changes: 8 additions & 8 deletions frontend/src/shared/remotes/axios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios';
import { postRefreshAccessToken } from './auth';
import { postRefreshAccessToken } from '@/features/auth/remotes/auth';
import type { AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';

const { BASE_URL } = process.env;
Expand Down Expand Up @@ -28,24 +28,24 @@ const setToken = (config: InternalAxiosRequestConfig) => {

// 응답 인터셉터
const refreshAccessTokenOnAuthError = async (error: AxiosError) => {
const config = error.config;
const originalRequest = error.config;

if (error.response?.status === 401 && config?.headers.Authorization) {
if (error.response?.status === 401 && originalRequest?.headers.Authorization) {
try {
const staleAccessToken = localStorage.getItem('userToken') ?? '';
const { accessToken } = await postRefreshAccessToken(staleAccessToken);
const { accessToken } = await postRefreshAccessToken();

localStorage.setItem('userToken', accessToken);
config.headers.Authorization = `Bearer ${accessToken}`;
originalRequest.headers.Authorization = `Bearer ${accessToken}`;

return client(config);
return client(originalRequest);
} catch {
// window.alert('세션이 만료되었습니다. 다시 로그인 해주세요');
window.alert('세션이 만료되었습니다. 다시 로그인 해주세요');
}
}

return Promise.reject(error);
};

clientBasic.interceptors.request.use(setToken);
client.interceptors.request.use(setToken);
client.interceptors.response.use((response) => response, refreshAccessTokenOnAuthError);

0 comments on commit 4f858e9

Please sign in to comment.