Skip to content

Commit

Permalink
fix: 쿠키 판단 로직 재건
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene028 committed Aug 7, 2024
1 parent d6c5642 commit ad6d611
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
19 changes: 15 additions & 4 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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,
headers: { 'Content-Type': 'application/json' },
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;
3 changes: 0 additions & 3 deletions src/components/auth/guard/MypageAccessGuard.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Outlet } from 'react-router-dom';

export default function MypageAccessGuard() {
return <></>;
// return <Navigate to={getAuthRedirectPat

return <Outlet />;
}
6 changes: 6 additions & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 2 additions & 7 deletions src/utils/storage/key.ts
Original file line number Diff line number Diff line change
@@ -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'
}

0 comments on commit ad6d611

Please sign in to comment.