diff --git a/frontend/src/utils/auth/AuthUtils.ts b/frontend/src/utils/auth/AuthUtils.ts index 335317aa7..2712bbc3a 100644 --- a/frontend/src/utils/auth/AuthUtils.ts +++ b/frontend/src/utils/auth/AuthUtils.ts @@ -14,6 +14,17 @@ export const getRefreshToken = () => { return localStorage.getItem('refresh_token') } +export const removeRefreshToken = () => { + return localStorage.removeItem('refresh_token') +} + +export const logout = () => { + removeRefreshToken(); + removeToken() + removeExToken(); + window.location.href = '/login' +} + export const removeToken = () => { return localStorage.removeItem('access_token') } diff --git a/frontend/src/utils/axios.ts b/frontend/src/utils/axios.ts index f17c7a7f3..52b66443c 100644 --- a/frontend/src/utils/axios.ts +++ b/frontend/src/utils/axios.ts @@ -1,7 +1,7 @@ import axiosLibrary from 'axios' import { refreshTokenApi } from 'api/auth/Auth' import { BASE_URL } from 'const/API' -import { getExToken, getToken, saveToken } from 'utils/auth/AuthUtils' +import {getExToken, getToken, logout, saveToken} from 'utils/auth/AuthUtils' const axios = axiosLibrary.create({ baseURL: BASE_URL, @@ -21,10 +21,18 @@ axios.interceptors.response.use( async (res) => res, async (error) => { if (error?.response?.status === 401) { - const { access_token } = await refreshTokenApi() - saveToken(access_token) - error.config.headers.Authorization = `Bearer ${access_token}` - return axiosLibrary(error.config) + try { + const { access_token } = await refreshTokenApi() + saveToken(access_token) + error.config.headers.Authorization = `Bearer ${access_token}` + return axiosLibrary(error.config) + } + catch (e: any) { + if (e?.response?.status === 400) { + logout(); + } + throw e; + } } return Promise.reject(error) },