From ff798a2bb5d2fdd0610d371a0c35c0f038fa5400 Mon Sep 17 00:00:00 2001 From: giho Date: Thu, 15 Aug 2024 01:46:40 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20main=ED=8E=98=EC=9D=B4=EC=A7=80=20?= =?UTF-8?q?=EB=B0=8F=20=ED=95=98=EC=9C=84=20=ED=8E=98=EC=9D=B4=EC=A7=80?= =?UTF-8?q?=EC=97=90=EC=84=9C=EB=A7=8C=20=EC=83=88=EB=A1=9C=EA=B3=A0?= =?UTF-8?q?=EC=B9=A8=EC=8B=9C=20=EC=97=91=EC=84=B8=EC=8A=A4=20=ED=86=A0?= =?UTF-8?q?=ED=81=B0=EC=9D=84=20=EB=B0=9C=EA=B8=89=EB=B0=9B=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 9 ++++++--- src/apis/instances/index.ts | 10 +++++----- src/authentication/accessTokenManager.ts | 4 ++-- .../Signin/_components/GoogleLoginButton.hook.tsx | 6 +++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 4a0bd9f..0067e60 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -17,7 +17,7 @@ import { useEffect } from 'react'; import { renewTokens } from './apis/services/auth'; // token -import AccessTokenManager from './authentication/AccessTokenManager'; +import accessTokenManager from './authentication/accessTokenManager'; export default function App() { // 모달 @@ -25,11 +25,14 @@ export default function App() { // 새로고침 시 엑세스 토큰 재발급 useEffect(() => { - if (!AccessTokenManager.hasAccessToken()) { + const isincludeMain = window.location.href.includes('main'); + const isHasAccessToken = accessTokenManager.hasAccessToken(); + + if (!isHasAccessToken && isincludeMain) { renewTokens() .then((newAccessToken) => { if (newAccessToken) { - AccessTokenManager.setAccessToken(newAccessToken); + accessTokenManager.setAccessToken(newAccessToken); } }) .catch((error) => { diff --git a/src/apis/instances/index.ts b/src/apis/instances/index.ts index 3548377..ad4d655 100644 --- a/src/apis/instances/index.ts +++ b/src/apis/instances/index.ts @@ -2,7 +2,7 @@ import axios, { AxiosError } from 'axios'; import { handleAxiosError } from '../../utils/handleAxiosError'; import { handleUnexpectedError } from '../../utils/handleUnexpectedError'; import { renewTokens } from '../services/auth'; -import AccessTokenManager from '@/authentication/accessTokenManager'; +import accessTokenManager from '@/authentication/accessTokenManager'; const baseURL = process.env.API_URL; @@ -28,10 +28,10 @@ export const axiosInstanceWithToken = axios.create({ // request interceptor의 경우 token을 넣을 때 자주 사용한다. axiosInstanceWithToken.interceptors.request.use( (AxiosRequestConfig) => { - const accessToken = AccessTokenManager.getAccessToken(); + const accessToken = accessTokenManager.getAccessToken(); // 만약 토큰이 존재하는 경우 헤더에 넣어준다. - if (AccessTokenManager.hasAccessToken()) { + if (accessTokenManager.hasAccessToken()) { AxiosRequestConfig.headers['authorization'] = `Bearer ${accessToken}`; } return AxiosRequestConfig; @@ -59,9 +59,9 @@ axiosInstanceWithToken.interceptors.response.use( // accessToken 갱신 const newAccessToken = await renewTokens(); - AccessTokenManager.setAccessToken(newAccessToken); + accessTokenManager.setAccessToken(newAccessToken); - const accessToken = AccessTokenManager.getAccessToken(); + const accessToken = accessTokenManager.getAccessToken(); if (originalRequest) { // 새로운 accessToken으로 요청 재시도 diff --git a/src/authentication/accessTokenManager.ts b/src/authentication/accessTokenManager.ts index de8f826..ab86362 100644 --- a/src/authentication/accessTokenManager.ts +++ b/src/authentication/accessTokenManager.ts @@ -1,4 +1,4 @@ -const AccessTokenManager = (function () { +const accessTokenManager = (function () { let accessToken: string | null = null; return { @@ -20,4 +20,4 @@ const AccessTokenManager = (function () { }; })(); -export default AccessTokenManager; +export default accessTokenManager; diff --git a/src/pages/Signin/_components/GoogleLoginButton.hook.tsx b/src/pages/Signin/_components/GoogleLoginButton.hook.tsx index 70f70c2..1b691b6 100644 --- a/src/pages/Signin/_components/GoogleLoginButton.hook.tsx +++ b/src/pages/Signin/_components/GoogleLoginButton.hook.tsx @@ -8,7 +8,7 @@ import { useGoogleOAuthTokenStore } from '../../../store/useAuthStore'; import { useUserStore } from '../../../store/useUserStore'; // token -import AccessTokenManager from '@/authentication/AccessTokenManager'; +import accessTokenManager from '@/authentication/accessTokenManager'; export default function useGoogleLoginButton() { const navigate = useNavigate(); @@ -20,11 +20,11 @@ export default function useGoogleLoginButton() { try { await axiosInstanceWithToken.post('/auth/google/callback', { code }).then((response) => { setGoogleOAuthToken(response.data.googleAccessToken); - AccessTokenManager.setAccessToken(response.data.applicationAccessToken); + accessTokenManager.setAccessToken(response.data.applicationAccessToken); setUser(response.data.user); }); - if (AccessTokenManager.hasAccessToken()) { + if (accessTokenManager.hasAccessToken()) { navigate('/main'); } } catch (error) {