diff --git a/public/index.html b/public/index.html index f6ace053..92543d29 100644 --- a/public/index.html +++ b/public/index.html @@ -2,7 +2,7 @@ - + { @@ -48,11 +48,13 @@ export const Header = (props) => { const [src, setSrc] = useRecoilState(avatar); const [userId, setUserId] = useRecoilState(id); const [userName, setUserName] = useRecoilState(name); - const setIsLogin = useSetRecoilState(isLogin); + const [Login, setLogin] = useRecoilState(isLogin); React.useEffect(() => { - setSrc(localStorage.avatar); - setUserId(localStorage.id); - setUserName(localStorage.name); + if (Login) { + setSrc(localStorage.avatar); + setUserId(localStorage.id); + setUserName(localStorage.name); + } }, []); const handleOpenNavMenu = (event) => { setAnchorElNav(event.currentTarget); @@ -76,7 +78,7 @@ export const Header = (props) => { "", { withCredentials: true }, ); - setIsLogin(false); + setLogin(false); localStorage.setItem("id", "guest"); localStorage.setItem("name", "guest"); localStorage.setItem("avatar", ""); diff --git a/src/pages/MainPage.js b/src/pages/MainPage.js index f994f518..55af9ff2 100644 --- a/src/pages/MainPage.js +++ b/src/pages/MainPage.js @@ -25,7 +25,13 @@ function MainPage() { const setIsLogin = useSetRecoilState(isLogin); const checkIsLogin = async () => { - setIsLogin(await checkTokenValid()); + const loggedIn = await checkTokenValid(); + if (!loggedIn) { + localStorage.removeItem("id"); + localStorage.removeItem("name"); + localStorage.removeItem("avatar"); + } + setIsLogin(loggedIn); }; useEffect(() => {