From cd5707f78a45196df6a29e84ac3174c8f9ffd361 Mon Sep 17 00:00:00 2001 From: Ethen1264 Date: Wed, 21 Aug 2024 22:25:58 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9BFix=20token=20refresh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/member.ts | 6 +++++- src/utils/Libs/apiClient.ts | 39 +++++++++++++++++++++++++++++++++++- src/utils/Libs/getRefresh.ts | 7 ++++++- src/utils/Libs/getToken.ts | 4 +++- 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/api/member.ts b/src/api/member.ts index d520488b..48f68714 100644 --- a/src/api/member.ts +++ b/src/api/member.ts @@ -1,6 +1,7 @@ import { GetServerSidePropsContext } from 'next'; import { toast } from 'react-toastify'; import { apiClient } from 'utils/Libs/apiClient'; +import { removeToken } from 'utils/Libs/removeToken'; import { MemberController } from 'utils/Libs/requestUrls'; import { setToken } from 'utils/Libs/setToken'; @@ -145,7 +146,10 @@ export const tokenReissue = async ( refreshToken = data.refreshToken; setToken(newAuthorization, refreshToken, ctx); return { newAuthorization }; - } catch (e: any) {} + } catch (e: any) { + removeToken(); + window.location.href = '/signin'; + } }; export const postProfileImage = async (image: Blob | string) => { diff --git a/src/utils/Libs/apiClient.ts b/src/utils/Libs/apiClient.ts index 894a2239..7cbeed88 100644 --- a/src/utils/Libs/apiClient.ts +++ b/src/utils/Libs/apiClient.ts @@ -1,10 +1,47 @@ import axios from 'axios'; import BASE_HEADER from '../Config/Config.json'; import { getRefresh } from './getRefresh'; +import { tokenReissue } from 'api/member'; +import { MemberController } from './requestUrls'; +import { setToken } from './setToken'; +import { getToken } from './getToken'; export const apiClient = axios.create({ baseURL: process.env.NEXT_PUBLIC_BASE_URL, headers: BASE_HEADER, }); -apiClient.interceptors.request.use(getRefresh); +apiClient.interceptors.response.use( + function (response) { + return response; + }, + async function (err) { + const originalRequest = err.config; + + if ( + err.response && + (err.response.status === 401 || err.response.status === 403) && + !originalRequest._retry + ) { + originalRequest._retry = true; + + try { + const { RefreshToken } = await getToken(null); + const tokenResponse = await tokenReissue( + 'Bearer ' + RefreshToken, + null + ); + + if (tokenResponse && tokenResponse.newAuthorization) { + originalRequest.headers['Authorization'] = + 'Bearer ' + tokenResponse.newAuthorization; + return apiClient(originalRequest); + } + } catch (refreshError) { + return Promise.reject(refreshError); + } + } + + return Promise.reject(err); + } +); diff --git a/src/utils/Libs/getRefresh.ts b/src/utils/Libs/getRefresh.ts index b595c423..7e9f8be9 100644 --- a/src/utils/Libs/getRefresh.ts +++ b/src/utils/Libs/getRefresh.ts @@ -1,12 +1,17 @@ import { AxiosRequestConfig } from 'axios'; import { tokenReissue } from '../../api/member'; import { getToken } from './getToken'; +import { MemberController } from './requestUrls'; export const getRefresh = async (config: AxiosRequestConfig) => { if (typeof window !== 'object') return config; const { Authorization, RefreshToken } = await getToken(null); - if (config.headers && Authorization) { + if ( + config.headers && + Authorization && + !config.url?.includes(MemberController.auth) + ) { config.headers['Authorization'] = Authorization; } else if ( !Authorization && diff --git a/src/utils/Libs/getToken.ts b/src/utils/Libs/getToken.ts index fae1bdb9..e2e72ccf 100644 --- a/src/utils/Libs/getToken.ts +++ b/src/utils/Libs/getToken.ts @@ -10,7 +10,9 @@ export const getToken = async (ctx: GetServerSidePropsContext | null) => { if (!RefreshToken) return {}; else if (!Authorization) { const { newAuthorization }: any = await tokenReissue(RefreshToken, ctx); - Authorization = newAuthorization; + if (newAuthorization) { + Authorization = newAuthorization; + } } return { Authorization, RefreshToken }; From c67affab53f07769b8e060b1d65e936a8c426cb9 Mon Sep 17 00:00:00 2001 From: Ethen1264 Date: Thu, 22 Aug 2024 22:46:59 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=9BFix:=20insert=20token=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/member.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/api/member.ts b/src/api/member.ts index 35088395..8d308141 100644 --- a/src/api/member.ts +++ b/src/api/member.ts @@ -4,7 +4,6 @@ import { apiClient } from 'utils/Libs/apiClient'; import { removeToken } from 'utils/Libs/removeToken'; import { MemberController } from 'utils/Libs/requestUrls'; import { setToken } from 'utils/Libs/setToken'; -import { setCookie } from 'nookies'; export const signin = async (id: string, password: string) => { try { @@ -150,10 +149,14 @@ export const tokenReissue = async ( } ); newAuthorization = data.accessToken; - setCookie(ctx, 'Authorization', `Bearer ${newAuthorization}`, { - expires: data.accessExp, - path: '/', - }); + refreshToken = data.refreshToken; + setToken( + data.accessToken, + data.accessExp, + data.refreshToken, + data.refreshExp, + null + ); return { newAuthorization }; } catch (e: any) { removeToken(); From 3feb7aba93a2a80aed7f731f951beb9e60eef906 Mon Sep 17 00:00:00 2001 From: Ethen1264 Date: Thu, 22 Aug 2024 22:49:48 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9BFix:=20add=20window=20object=20?= =?UTF-8?q?type=20verification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/member.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/api/member.ts b/src/api/member.ts index 8d308141..ce5ee615 100644 --- a/src/api/member.ts +++ b/src/api/member.ts @@ -160,7 +160,9 @@ export const tokenReissue = async ( return { newAuthorization }; } catch (e: any) { removeToken(); - window.location.href = '/signin'; + if (typeof window !== 'undefined') { + window.location.href = '/signin'; + } } }; From 0cbff99b2d2c60e56dd0d7ac34e7c8d4090536b4 Mon Sep 17 00:00:00 2001 From: Ethen1264 Date: Thu, 22 Aug 2024 22:54:00 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=A5Delete:=20delete=20unnecessary?= =?UTF-8?q?=20token=20verification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/Libs/getToken.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/utils/Libs/getToken.ts b/src/utils/Libs/getToken.ts index e2e72ccf..8a5aeefb 100644 --- a/src/utils/Libs/getToken.ts +++ b/src/utils/Libs/getToken.ts @@ -6,15 +6,6 @@ export const getToken = async (ctx: GetServerSidePropsContext | null) => { if (ctx) { let Authorization = ctx.req.cookies['Authorization'] || ''; let RefreshToken = ctx.req.cookies['RefreshToken'] || ''; - - if (!RefreshToken) return {}; - else if (!Authorization) { - const { newAuthorization }: any = await tokenReissue(RefreshToken, ctx); - if (newAuthorization) { - Authorization = newAuthorization; - } - } - return { Authorization, RefreshToken }; } else { const { Authorization, RefreshToken } = parseCookies();