fix: auth retry
This commit is contained in:
1 parent
fbbd7b52e3
commit
bbae7abaf5
3 files changed
+17
-7
No files matched your search
@@ -26,7 +26,7 @@ const Container: FC<Props> = ({ page }) => {
|
|||||||
<div className='h-2 w-full' />
|
<div className='h-2 w-full' />
|
||||||
</div>
|
</div>
|
||||||
<div className='hidden w-full rounded-lg xs:block'>
|
<div className='hidden w-full rounded-lg xs:block'>
|
||||||
<Trending showId={movie.id} showType={movie.type} />
|
<Trending showId={movie?.id} showType={movie?.type} />
|
||||||
<div className='h-2 w-full' />
|
<div className='h-2 w-full' />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ import tokenMutation from '@/data/mutations/token';
|
|||||||
import { useSetSession } from '@/hooks/useSession';
|
import { useSetSession } from '@/hooks/useSession';
|
||||||
import { useToast } from '@/hooks/useToast';
|
import { useToast } from '@/hooks/useToast';
|
||||||
|
|
||||||
|
const AUTH_RETRIES = 10;
|
||||||
|
|
||||||
const useAuth = () => {
|
const useAuth = () => {
|
||||||
const setSession = useSetSession();
|
const setSession = useSetSession();
|
||||||
const tabRef = useRef<Window | null>(null);
|
const tabRef = useRef<Window | null>(null);
|
||||||
const intervalRef = useRef<NodeJS.Timeout>();
|
const intervalRef = useRef<NodeJS.Timeout>();
|
||||||
const retryRef = useRef(10);
|
const retryRef = useRef(AUTH_RETRIES);
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
@@ -28,15 +30,13 @@ const useAuth = () => {
|
|||||||
tabRef.current?.close();
|
tabRef.current?.close();
|
||||||
},
|
},
|
||||||
onError() {
|
onError() {
|
||||||
if (!retryRef.current) {
|
if (!--retryRef.current) {
|
||||||
toast({ title: 'Authorization failed', description: 'Try to authorize later' });
|
toast({ title: 'Authorization failed', description: 'Try to authorize later' });
|
||||||
|
|
||||||
tabRef.current?.close();
|
tabRef.current?.close();
|
||||||
|
|
||||||
return clearInterval(intervalRef.current);
|
return clearInterval(intervalRef.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
retryRef.current--;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -49,6 +49,7 @@ const useAuth = () => {
|
|||||||
onSuccess({ redirectUrl, requestToken }) {
|
onSuccess({ redirectUrl, requestToken }) {
|
||||||
if (!tabRef.current) return;
|
if (!tabRef.current) return;
|
||||||
|
|
||||||
|
retryRef.current = AUTH_RETRIES;
|
||||||
tabRef.current.location = redirectUrl;
|
tabRef.current.location = redirectUrl;
|
||||||
|
|
||||||
intervalRef.current = setInterval(() => session.mutate({ requestToken }), 2000);
|
intervalRef.current = setInterval(() => session.mutate({ requestToken }), 2000);
|
||||||
|
|||||||
+11
-2
@@ -1,8 +1,11 @@
|
|||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
import { isServer } from '@tanstack/react-query';
|
||||||
import { atom, useAtomValue, useSetAtom } from 'jotai';
|
import { atom, useAtomValue, useSetAtom } from 'jotai';
|
||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
|
|
||||||
const sessionAtom = atom<string | undefined>(Cookies.get('session') as string);
|
const sessionAtom = atom<string | undefined>(
|
||||||
|
(isServer ? Cookies.get('session') : localStorage.getItem('session')) as string
|
||||||
|
);
|
||||||
|
|
||||||
export const useSession = () => useAtomValue(sessionAtom);
|
export const useSession = () => useAtomValue(sessionAtom);
|
||||||
|
|
||||||
@@ -11,7 +14,13 @@ export const useSetSession = () => {
|
|||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
(session: string | undefined) => {
|
(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(session);
|
||||||
},
|
},
|
||||||
[setSession]
|
[setSession]
|
||||||
|
|||||||
Reference in new issue
Block a user