diff --git a/app/(site)/(root)/container.tsx b/app/(site)/(root)/container.tsx index a21a45f..28a0542 100644 --- a/app/(site)/(root)/container.tsx +++ b/app/(site)/(root)/container.tsx @@ -26,7 +26,7 @@ const Container: FC = ({ page }) => {
- +
diff --git a/components/Header/components/User/useAuth.ts b/components/Header/components/User/useAuth.ts index 25666ac..b7d6646 100644 --- a/components/Header/components/User/useAuth.ts +++ b/components/Header/components/User/useAuth.ts @@ -6,11 +6,13 @@ import tokenMutation from '@/data/mutations/token'; import { useSetSession } from '@/hooks/useSession'; import { useToast } from '@/hooks/useToast'; +const AUTH_RETRIES = 10; + const useAuth = () => { const setSession = useSetSession(); const tabRef = useRef(null); const intervalRef = useRef(); - const retryRef = useRef(10); + const retryRef = useRef(AUTH_RETRIES); const { toast } = useToast(); @@ -28,15 +30,13 @@ const useAuth = () => { tabRef.current?.close(); }, onError() { - if (!retryRef.current) { + if (!--retryRef.current) { toast({ title: 'Authorization failed', description: 'Try to authorize later' }); tabRef.current?.close(); return clearInterval(intervalRef.current); } - - retryRef.current--; } }); @@ -49,6 +49,7 @@ const useAuth = () => { onSuccess({ redirectUrl, requestToken }) { if (!tabRef.current) return; + retryRef.current = AUTH_RETRIES; tabRef.current.location = redirectUrl; intervalRef.current = setInterval(() => session.mutate({ requestToken }), 2000); diff --git a/hooks/useSession.ts b/hooks/useSession.ts index b676089..1db926f 100644 --- a/hooks/useSession.ts +++ b/hooks/useSession.ts @@ -1,8 +1,11 @@ import { useCallback } from 'react'; +import { isServer } from '@tanstack/react-query'; import { atom, useAtomValue, useSetAtom } from 'jotai'; import Cookies from 'js-cookie'; -const sessionAtom = atom(Cookies.get('session') as string); +const sessionAtom = atom( + (isServer ? Cookies.get('session') : localStorage.getItem('session')) as string +); export const useSession = () => useAtomValue(sessionAtom); @@ -11,7 +14,13 @@ export const useSetSession = () => { return useCallback( (session: string | undefined) => { - typeof session === 'string' ? Cookies.set('session', session) : Cookies.remove('session'); + if (typeof session === 'string') { + localStorage.setItem('session', session); + Cookies.set('session', session); + } else { + localStorage.removeItem('session'); + Cookies.remove('session'); + } setSession(session); }, [setSession]