Skip to content

Commit

Permalink
refact, fixed: useLogout hooks fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
5nxtnxtnxt committed Dec 11, 2023
1 parent e943355 commit 324932a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 21 deletions.
36 changes: 36 additions & 0 deletions front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@types/three": "^0.158.1",
"axios": "^1.6.2",
"react": "^18.2.0",
"react-cookie": "^6.1.1",
"react-dom": "^18.2.0",
"react-router-dom": "^6.18.0",
"styled-components": "^6.1.0",
Expand Down
13 changes: 9 additions & 4 deletions front/src/hooks/useLogout.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import cookie from 'react-cookies';
import { useCookies } from 'react-cookie';

const useLogout = () => {
window.open(`/api/auth/logout`, '_self');
cookie.remove('loggedin');
window.location.replace('/');
const [, , removeCookie] = useCookies(['loggedin']);
const logout = () => {
window.open(`/api/auth/logout`, '_self');
removeCookie('loggedin', { path: '/' });
window.location.replace('/');
};

return logout;
};

export default useLogout;
5 changes: 3 additions & 2 deletions front/src/pages/Intro/Intro.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import cookie from 'react-cookies';
import { useCookies } from 'react-cookie';
import styled from 'styled-components';
import { SnowGlobeCanvas, UIContainer } from '@components';
import mockData from '@mock';
Expand All @@ -23,10 +23,11 @@ const TitleDiv = styled.div`
const Intro = () => {
const navigate = useNavigate();
const { setMessageList } = useContext(MessageListContext);
const [cookie] = useCookies(['loggedin']);

useEffect(() => {
setMessageList(mockData.snowball_data.message_list as Array<Message>);
cookie.load('loggedin') ? navigate('/main') : null;
cookie.loggedin ? navigate('/main') : null;
}, [setMessageList, navigate]);

return (
Expand Down
29 changes: 15 additions & 14 deletions front/src/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect, useRef, useContext, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import axios from 'axios';
import cookie from 'react-cookies';
import styled from 'styled-components';
import { Loading } from '@utils';
import { useLogout } from '@hooks';
Expand All @@ -14,6 +13,7 @@ import {
SnowBallData
} from '@pages/Visit/SnowBallProvider';
import { MessageListContext, Message } from '@pages/Visit/MessageListProvider';
import { useCookies } from 'react-cookie';

const MainBodyWrap = styled.div`
width: 100%;
Expand Down Expand Up @@ -66,13 +66,14 @@ const moveSnowball = (

const Main = () => {
const navigate = useNavigate();
const logout = useLogout;
const logout = useLogout();
const { setSnowBallData, setUserData, userData, snowBallData } =
useContext(SnowBallContext);
const { setMessageList } = useContext(MessageListContext);
const leftArrowRef = useRef<HTMLImageElement>(null);
const rightArrowRef = useRef<HTMLImageElement>(null);
const [isLoading, setIsLoading] = useState(false);
const [cookie] = useCookies(['loggedin']);

const delayButton = () => {
if (leftArrowRef.current && rightArrowRef.current) {
Expand All @@ -92,21 +93,21 @@ const Main = () => {
}
};

// const saveCookie = () => {
// const cookieToken = import.meta.env.VITE_APP_COOKIE_TOKEN;
// const cookieName = 'access_token';
// const cookieValue = cookieToken;
// const today = new Date();
// const expire = new Date();
// const secure = true;
// expire.setDate(today.getDate() + 1);
// document.cookie = `${cookieName}=${cookieValue}; expires=${expire.toUTCString()}; secure=${secure}; path=/`;
// };
const saveCookie = () => {
const cookieToken = import.meta.env.VITE_APP_COOKIE_TOKEN;
const cookieName = 'access_token';
const cookieValue = cookieToken;
const today = new Date();
const expire = new Date();
const secure = true;
expire.setDate(today.getDate() + 1);
document.cookie = `${cookieName}=${cookieValue}; expires=${expire.toUTCString()}; secure=${secure}; path=/`;
};

useEffect(() => {
// saveCookie();
saveCookie();

if (!cookie.load('loggedin')) {
if (!cookie.loggedin) {
navigate('/');
return;
}
Expand Down
2 changes: 1 addition & 1 deletion front/src/pages/Main/MenuModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const StyledClosed = styled(StyledSection)`
const MenuModal = (props: ModalProps) => {
const { userData } = useContext(SnowBallContext);
const navigate = useNavigate();
const logout = useLogout;
const logout = useLogout();

const makeNewSnowBall = () => {
navigate('/make/snowball');
Expand Down

0 comments on commit 324932a

Please sign in to comment.