From 3814787cc9e74c09fca799e155cec89ac1cbf4d4 Mon Sep 17 00:00:00 2001 From: seung365 Date: Thu, 8 Aug 2024 03:50:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=94=94=EB=B2=84=EA=B9=85=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Provider/Auth.tsx | 42 ++++++++------------------ src/routes/components/PrivateRoute.tsx | 4 ++- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/Provider/Auth.tsx b/src/Provider/Auth.tsx index 7066fd9..d511896 100644 --- a/src/Provider/Auth.tsx +++ b/src/Provider/Auth.tsx @@ -1,51 +1,35 @@ import { AuthContextType, AuthInfo } from '@/types'; -import { createContext, useContext, useState, ReactNode, useEffect, useCallback } from 'react'; +import { createContext, useContext, useState, ReactNode, useEffect } from 'react'; export const AuthContext = createContext(undefined); export const AuthProvider = ({ children }: { children: ReactNode }) => { const [authInfo, setAuthInfo] = useState(undefined); - const updateAuthInfo = useCallback((auth: AuthInfo) => { - setAuthInfo((prevAuth) => { - if (JSON.stringify(prevAuth) !== JSON.stringify(auth)) { - return auth; - } - return prevAuth; - }); - }, []); + const updateAuthInfo = (auth: AuthInfo) => { + if (auth) { + setAuthInfo(auth); + } + }; - const handleAuthInfo = useCallback(() => { + const handleAuthInfo = () => { const authToken = sessionStorage.getItem('accessToken'); if (authToken) { const email = sessionStorage.getItem('email') || ''; const name = sessionStorage.getItem('name') || ''; const picture = sessionStorage.getItem('picture') || ''; - updateAuthInfo({ + setAuthInfo({ accessToken: authToken, - email, - name, - picture, + email: email, + name: name, + picture: picture, }); - } else { - setAuthInfo(undefined); } - }, [updateAuthInfo]); + }; useEffect(() => { handleAuthInfo(); - - const handleStorageChange = (event: StorageEvent) => { - if (event.key === 'accessToken') { - handleAuthInfo(); - } - }; - - window.addEventListener('storage', handleStorageChange); - return () => { - window.removeEventListener('storage', handleStorageChange); - }; - }, [handleAuthInfo]); + }); return ( {children} diff --git a/src/routes/components/PrivateRoute.tsx b/src/routes/components/PrivateRoute.tsx index 795080d..74a6a9f 100644 --- a/src/routes/components/PrivateRoute.tsx +++ b/src/routes/components/PrivateRoute.tsx @@ -3,10 +3,12 @@ import { Navigate, Outlet } from 'react-router-dom'; import { getDynamicPath } from '../path'; export const PrivateRoute = () => { - const authInfo = useAuth(); + const { authInfo } = useAuth(); if (!authInfo) { return ; + } else { + console.log(authInfo.accessToken); } return ;