From 2538338c111967352c7fcdd9b3eafc8978713938 Mon Sep 17 00:00:00 2001 From: YoujungSon Date: Mon, 27 Mar 2023 17:44:22 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20instance=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/assetInstance.ts | 18 +++++++++++++++++- src/apis/auth.ts | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/apis/assetInstance.ts b/src/apis/assetInstance.ts index f3eb178..d70b2f7 100644 --- a/src/apis/assetInstance.ts +++ b/src/apis/assetInstance.ts @@ -7,6 +7,12 @@ export const AxiosInstance = axios.create({ Accept: 'application/json', }, }); +export const AuthAxiosInstance = axios.create({ + baseURL: process.env.REACT_APP_BASE_URL, + headers: { + Accept: 'application/json', + }, +}); AxiosInstance.interceptors.request.use( function (config) { @@ -40,11 +46,21 @@ AxiosInstance.interceptors.response.use( AxiosInstance.defaults.headers.common.Authorization = `Bearer ${tokenData.token}`; return AxiosInstance(originalRequest); } - if (error.response.data.errorMessage === 'The token is incorrect. Please login again.') { + + return Promise.reject(error); + }, +); +AuthAxiosInstance.interceptors.response.use( + function (response) { + return response; + }, + async (error) => { + if (error.response.status === 401) { alert('로그인이 만료되었습니다. 다시 로그인해주세요.'); localStorage.clear(); window.location.href = '/'; } + return Promise.reject(error); }, ); diff --git a/src/apis/auth.ts b/src/apis/auth.ts index 4e2d247..0b77b7c 100644 --- a/src/apis/auth.ts +++ b/src/apis/auth.ts @@ -1,4 +1,4 @@ -import { AxiosInstance } from './assetInstance'; +import { AuthAxiosInstance, AxiosInstance } from './assetInstance'; type readuserType = { token: string; @@ -7,7 +7,7 @@ type readuserType = { }; export const refreshToken = async (token: string) => { - const response = await AxiosInstance.post('/authtoken', { token }); + const response = await AuthAxiosInstance.post('/authtoken', { token }); return response; }; export const readuser = async ({ token, email, password }: readuserType) => {