diff --git a/src/apis/index.ts b/src/apis/index.ts index a09ed77..17235d2 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -1,6 +1,7 @@ import { BASE_URL, DEV_AUTH_TOKEN } from '@/constants/environment'; -import useAuthToken from '@/hooks/auth/useAuthToken'; +import { getCookie } from '@/utils/auth'; import axios from 'axios'; +import { CookieKeys } from '@/utils/storage/key'; const apiClient = axios.create({ baseURL: BASE_URL, @@ -8,8 +9,18 @@ const apiClient = axios.create({ withCredentials: true }); -apiClient.defaults.headers.common['Authorization'] = DEV_AUTH_TOKEN - ? `${DEV_AUTH_TOKEN}` - : `Bearer ${useAuthToken().accessToken}`; +export function setAuthHeader() { + const accessToken = getCookie(CookieKeys.AccessToken); + + if (DEV_AUTH_TOKEN) { + apiClient.defaults.headers.common['Authorization'] = DEV_AUTH_TOKEN; + } else { + apiClient.defaults.headers.common['Authorization'] = accessToken + ? `Bearer ${accessToken}` + : ''; + } +} + +setAuthHeader(); export default apiClient; diff --git a/src/components/auth/guard/MypageAccessGuard.tsx b/src/components/auth/guard/MypageAccessGuard.tsx index b98600b..8a6f99e 100644 --- a/src/components/auth/guard/MypageAccessGuard.tsx +++ b/src/components/auth/guard/MypageAccessGuard.tsx @@ -1,8 +1,5 @@ import { Outlet } from 'react-router-dom'; export default function MypageAccessGuard() { - return <>; - // return ; } diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 84d1148..f088b68 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -19,10 +19,16 @@ export function getCookie(name: string): string { return ''; } +export const isAuthenticated = () => { + const token = getCookie('accessToken'); + return !!token; +}; + export function deleteCookie(name: string) { document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; } +//TODO: 서버에서 로그아웃 로직 생성할 예정 export function logout() { useAuthToken().clearToken(); sessionStorage.clear(); diff --git a/src/utils/storage/key.ts b/src/utils/storage/key.ts index fa7413f..37f85ab 100644 --- a/src/utils/storage/key.ts +++ b/src/utils/storage/key.ts @@ -1,9 +1,4 @@ -export const enum StorageKeys { - AccessToken = 'access_token', - RefreshToken = 'refresh_token' -} - export const enum CookieKeys { - AccessToken = 'access', - RefreshToken = 'refresh' + AccessToken = 'accessToken', + RefreshToken = 'refreshToken' }