Skip to content

Commit

Permalink
fix: 로그인 유지 시간 sessionStorage에서 확인
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene028 committed Aug 7, 2024
1 parent 17a8c6d commit 3fa3455
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
8 changes: 0 additions & 8 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { BASE_URL, DEV_AUTH_TOKEN } from '@/constants/environment';
import { getCookie } from '@/utils/auth';
import axios from 'axios';
import { CookieKeys } from '@/utils/storage/key';

const apiClient = axios.create({
baseURL: BASE_URL,
Expand All @@ -10,14 +8,8 @@ const apiClient = axios.create({
});

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}`
: '';
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/components/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import { color } from 'wowds-tokens';
import { media } from '@/styles';
import styled from '@emotion/styled';
import { useNavigate } from 'react-router-dom';
import { isAuthenticated } from '@/utils/auth';

//TODO: 백엔드 로그인 로직 수정 이후 반영 필요
export default function Header() {
const navigation = useNavigate();

const handleClick = () => {
navigation(RoutePath.Dashboard);
if (isAuthenticated()) navigation(RoutePath.Dashboard);
else {
navigation(RoutePath.GithubSignin);
}
};

return (
Expand All @@ -26,9 +30,11 @@ export default function Header() {
<HeaderLogo />
</Flex>
</LogoContainer>
<JoinButton onClick={handleClick}>내 정보</JoinButton>

<JoinButton onClick={handleClick}>로그인/가입하기</JoinButton>
{isAuthenticated() ? (
<JoinButton onClick={handleClick}>내 정보</JoinButton>
) : (
<JoinButton onClick={handleClick}>로그인/가입하기</JoinButton>
)}
</HeaderContainter>
</Container>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/myPage/BasicUserInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const BasicUserInfo = ({ member }: { member: User }) => {
const handleLogoutClick = () => {
logout();
navigate('/');
location.reload();
};

return (
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { Link } from 'react-router-dom';
/** 깃허브 로그인 및 가입하기 */
export const Auth = () => {
const handleClick = () => {
//TODO: QA용으로 임시로 설정
sessionStorage.setItem('isLogin', 'true');

// GitHub 로그인 페이지로 직접 리다이렉트
setTimeout(function () {
document.location.href = RoutePath.AuthGithubLoginRedirect;
Expand Down
5 changes: 5 additions & 0 deletions src/pages/redirect/AuthServerRedirectNavigate.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import RoutePath from '@/routes/routePath';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
export const AuthServerRedirectNavigate = () => {
const navigate = useNavigate();

useEffect(() => {
sessionStorage.setItem('isLogin', 'true');
}, []);
navigate(RoutePath.Dashboard);

return null;
Expand Down
1 change: 0 additions & 1 deletion src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const router = sentryCreateBrowserRouter([
},
{
path: RoutePath.GithubSignin,
element: <AuthAccessGuard />,
children: [{ index: true, element: <Auth /> }]
},
{
Expand Down
10 changes: 6 additions & 4 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import useAuthToken from '@/hooks/auth/useAuthToken';

Check failure on line 1 in src/utils/auth.ts

View workflow job for this annotation

GitHub Actions / build

'useAuthToken' is declared but its value is never read.
import { CookieKeys } from './storage/key';

Check failure on line 2 in src/utils/auth.ts

View workflow job for this annotation

GitHub Actions / build

'CookieKeys' is declared but its value is never read.

/**
* 쿠키 이름을 기반으로 쿠키 값을 가져옴
Expand All @@ -7,6 +8,7 @@ import useAuthToken from '@/hooks/auth/useAuthToken';
*/
export function getCookie(name: string): string {
const cookieString: string = document.cookie;
console.log('쿠키스트링', cookieString);
const cookies: string[] = cookieString.split(';');

for (const cookie of cookies) {
Expand All @@ -20,8 +22,10 @@ export function getCookie(name: string): string {
}

export const isAuthenticated = () => {
const token = getCookie('accessToken');
return !!token;
const isLogin = sessionStorage.getItem('isLogin');

if (isLogin === 'true') return true;
else return false;
};

export function deleteCookie(name: string) {
Expand All @@ -30,7 +34,5 @@ export function deleteCookie(name: string) {

//TODO: 서버에서 로그아웃 로직 생성할 예정
export function logout() {
useAuthToken().clearToken();
sessionStorage.clear();
location.reload();
}

0 comments on commit 3fa3455

Please sign in to comment.