From bdd47048bf79b5ddfd29a15f150d6c5927d287f6 Mon Sep 17 00:00:00 2001 From: ldu1020 Date: Fri, 27 Oct 2023 10:09:18 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9BisLogin=20from=20useGlobalCo?= =?UTF-8?q?ntext=20=3D>=20useLogin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/contexts/global/hooks/useGlobalEffect.ts | 23 +------------------- src/contexts/global/hooks/useGlobalState.ts | 6 ----- src/hocs/withAuthGuard.tsx | 4 ++-- src/hocs/withUnAuthGuard.tsx | 4 ++-- src/hooks/useLogin.ts | 2 +- 5 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/contexts/global/hooks/useGlobalEffect.ts b/src/contexts/global/hooks/useGlobalEffect.ts index 0523a2af..91329d7e 100644 --- a/src/contexts/global/hooks/useGlobalEffect.ts +++ b/src/contexts/global/hooks/useGlobalEffect.ts @@ -12,28 +12,7 @@ interface useGlobalEffectParams extends ReturnType { webStorage: ReturnType; } -export const useGlobalEffect = ({ - webStorage, - dispatch, -}: useGlobalEffectParams) => { - // const { mutate } = useUserRefreshCreateMutation({ - // options: { - // enabled: !!tokenStorage, - // onSuccess: (res) => { - // tokenStorage?.set(res); - // }, - // onError: () => { - // tokenStorage?.remove(); - // }, - // }, - // }); - - // For: sync isLogin by access token - useEffect(() => { - const isLogin = !!webStorage.token?.access; - dispatch({ type: 'SET_IS_LOGIN', payload: isLogin }); - }, [dispatch, webStorage.token?.access]); - +export const useGlobalEffect = ({ dispatch }: useGlobalEffectParams) => { // For: refresh before expired useEffect(() => { const refresh = tokenStorage?.get()?.refresh; diff --git a/src/contexts/global/hooks/useGlobalState.ts b/src/contexts/global/hooks/useGlobalState.ts index 5a0bb719..c92eb566 100644 --- a/src/contexts/global/hooks/useGlobalState.ts +++ b/src/contexts/global/hooks/useGlobalState.ts @@ -5,13 +5,11 @@ import { createSlice } from '@/utils/react/create-slice'; import { ConfirmAlertState } from './types/confirm-alert-state'; export type GlobalStateType = { - isLogin: null | boolean; confirmAlert: ConfirmAlertState; count: number; }; const initialState: GlobalStateType = { - isLogin: null, count: 0, confirmAlert: { isOpen: false, @@ -25,10 +23,6 @@ const initialState: GlobalStateType = { export const globalSlice = createSlice({ initialState, reducers: { - RESET: () => initialState, - SET_IS_LOGIN: (state, isLogin: boolean) => { - state.isLogin = isLogin; - }, SET_COUNT: (state, count: number) => { state.count = count; }, diff --git a/src/hocs/withAuthGuard.tsx b/src/hocs/withAuthGuard.tsx index 06e479c1..37b614d0 100644 --- a/src/hocs/withAuthGuard.tsx +++ b/src/hocs/withAuthGuard.tsx @@ -2,7 +2,7 @@ import { ComponentProps, ComponentType, useEffect } from 'react'; import { useRouter } from 'next/router'; -import { useGlobalContext } from '@/contexts/global/useGlobalStoreContext'; +import { useLogin } from '@/hooks/useLogin'; import { ROUTES } from '@/constants/routes'; @@ -11,7 +11,7 @@ export default function withAuthGuard>( ) { return function WrappedAppComponent(props: ComponentProps) { const router = useRouter(); - const isLogin = useGlobalContext((ctx) => ctx.state.isLogin); + const { isLogin } = useLogin(); useEffect(() => { if (isLogin === false) diff --git a/src/hocs/withUnAuthGuard.tsx b/src/hocs/withUnAuthGuard.tsx index 15c3f59e..15241fe2 100644 --- a/src/hocs/withUnAuthGuard.tsx +++ b/src/hocs/withUnAuthGuard.tsx @@ -2,14 +2,14 @@ import { ComponentProps, ComponentType, useEffect } from 'react'; import { useRouter } from 'next/router'; -import { useGlobalContext } from '@/contexts/global/useGlobalStoreContext'; +import { useLogin } from '@/hooks/useLogin'; export default function withUnAuthGuard>( AppComponent: T, ) { return function WrappedAppComponent(props: ComponentProps) { const router = useRouter(); - const isLogin = useGlobalContext((store) => store.state.isLogin); + const { isLogin } = useLogin(); useEffect(() => { if (isLogin === true) diff --git a/src/hooks/useLogin.ts b/src/hooks/useLogin.ts index 1ecc4b35..55bc4d53 100644 --- a/src/hooks/useLogin.ts +++ b/src/hooks/useLogin.ts @@ -9,8 +9,8 @@ import { useGlobalContext } from '@/contexts/global/useGlobalStoreContext'; import { tokenStorage } from '@/utils/web-storage/token'; export const useLogin = () => { - const isLogin = useGlobalContext((ctx) => ctx.state.isLogin); const token = useGlobalContext((ctx) => ctx.webStorage.token); + const isLogin = !!token; const queryClient = useQueryClient(); const router = useRouter();