Skip to content

Commit

Permalink
Merge pull request #72 from taco-official/hotfix
Browse files Browse the repository at this point in the history
fix(hotfix): fix logout logic
  • Loading branch information
seoulyego authored Oct 16, 2024
2 parents 25064af + fcfd35f commit 7f8f313
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
12 changes: 1 addition & 11 deletions src/components/UserProfile/UserEditButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import React from 'react'
import { useNavigate } from 'react-router-dom'
import { Button } from 'antd'
import theme from '@styles/theme'
import kyMethod from '@constants/kyMethod'
import useKyMutation from '@hooks/useKyMutation'

function UserEditButton() {
const navigate = useNavigate()
const { mutateAsync } = useKyMutation(kyMethod.POST, 'logout', ['me'])

return (
<>
Expand All @@ -26,14 +23,7 @@ function UserEditButton() {
프로필 수정
</Button>
<Button
onClick={async () => {
try {
await mutateAsync()
navigate('/')
} catch {
alert('로그아웃 하지 못했습니다. 다시 시도해 주세요')
}
}}
onClick={() => navigate('/logout')}
type="text"
style={{
fontFamily: theme.style.main,
Expand Down
21 changes: 9 additions & 12 deletions src/hooks/useUserData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ import useLoginStore from '@stores/useLoginStore'
import useKyQuery from './useKyQuery'

const useUserData = () => {
const {
isLoading,
data: userData,
isError,
} = useKyQuery('me', undefined, {
staleTime: 1000 * 60 * 60,
gcTime: 1000 * 60 * 60,
const { isLoading, data: userData } = useKyQuery('me', undefined, {
staleTime: 1000 * 60 * 10,
gcTime: 1000 * 60 * 10,
select: (data) => data.data,
})
const loginStore = useLoginStore()

useEffect(() => {
if (isLoading) return
if (userData)
if (userData) {
if (!loginStore.isLogin) loginStore.setLogin(userData)
else if (!isEqual(loginStore.loginData, userData))
else if (loginStore.isLogin && !isEqual(userData, loginStore.loginData))
loginStore.setLoginData(userData)
if (isError && loginStore.isLogin) loginStore.setLogout()
}, [isLoading, userData, isError])
}
}, [userData])

return isLoading
}

export default useUserData
6 changes: 3 additions & 3 deletions src/pages/Layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import GlobalStyle from '@styles/GlobalStyle'
import useUserData from '@hooks/useUserData'
import NavBar from '@components/Navbar/NavBar'
import Footer from '@components/Footer/Footer'
import LoadingPage from './LoadingPage'

export default function Layout() {
useUserData()

const isLoading = useUserData()
return (
<>
<GlobalStyle />
<NavBar />
<StyledMain>
<RouterProvider router={router} />
{isLoading ? <LoadingPage /> : <RouterProvider router={router} />}
</StyledMain>
<Footer />
</>
Expand Down
22 changes: 22 additions & 0 deletions src/pages/LogoutPage/LogoutPage.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from 'react'
import { useNavigate } from 'react-router-dom'
import kyMethod from '@constants/kyMethod'
import useLoginStore from '@stores/useLoginStore'
import useKyMutation from '@hooks/useKyMutation'

function LogoutPage() {
const isLogin = useLoginStore((state) => state.isLogin)
const setLogout = useLoginStore((state) => state.setLogout)
const { mutate } = useKyMutation(kyMethod.POST, 'logout', ['me'])
const navigate = useNavigate()

useEffect(() => {
if (isLogin) {
setLogout()
mutate()
}
navigate('/')
}, [])
}

export default LogoutPage
2 changes: 2 additions & 0 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const FeedPage = lazy(() => import('./FeedPage/FeedPage'))
const MyPage = lazy(() => import('./UserPage/MyPage'))
const MyEditPage = lazy(() => import('./UserEditPage/UserEditPage'))
const UserPage = lazy(() => import('./UserPage/UserPage'))
const LogoutPage = lazy(() => import('./LogoutPage/LogoutPage'))

export {
HomePage,
Expand All @@ -16,4 +17,5 @@ export {
MyPage,
MyEditPage,
UserPage,
LogoutPage,
}
4 changes: 4 additions & 0 deletions src/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const routes = [
loader: loader.userLoader,
element: <pages.UserPage />,
},
{
path: 'logout',
element: <pages.LogoutPage />,
},
{
path: '*',
element: <ErrorPage />,
Expand Down

0 comments on commit 7f8f313

Please sign in to comment.