Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

navigate login when expried #130

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions frontend/src/utils/auth/AuthUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
18 changes: 13 additions & 5 deletions frontend/src/utils/axios.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
},
Expand Down