Refine admin auth handling
This commit is contained in:
parent
a79ec4f078
commit
14ccc20f7a
@ -22,8 +22,8 @@ export const hasAuthEmailCookie = () =>
|
|||||||
export const clearAuthEmailCookie = () =>
|
export const clearAuthEmailCookie = () =>
|
||||||
deleteCookie(KEY_AUTH_EMAIL);
|
deleteCookie(KEY_AUTH_EMAIL);
|
||||||
|
|
||||||
export const isCredentialsSignInError = (error: any) =>
|
export const isCredentialsSignInError = (error?: any) =>
|
||||||
(error.message || `${error}`).includes(KEY_CREDENTIALS_SIGN_IN_ERROR);
|
(error?.message || `${error}`).includes(KEY_CREDENTIALS_SIGN_IN_ERROR);
|
||||||
|
|
||||||
export const generateAuthSecret = () => fetch(
|
export const generateAuthSecret = () => fetch(
|
||||||
'https://generate-secret.vercel.app/32',
|
'https://generate-secret.vercel.app/32',
|
||||||
|
|||||||
@ -18,7 +18,6 @@ import { AdminData, getAdminDataAction } from '@/admin/actions';
|
|||||||
import {
|
import {
|
||||||
storeAuthEmailCookie,
|
storeAuthEmailCookie,
|
||||||
clearAuthEmailCookie,
|
clearAuthEmailCookie,
|
||||||
isCredentialsSignInError,
|
|
||||||
getAuthEmailCookie,
|
getAuthEmailCookie,
|
||||||
} from '@/auth';
|
} from '@/auth';
|
||||||
import { useRouter, usePathname } from 'next/navigation';
|
import { useRouter, usePathname } from 'next/navigation';
|
||||||
@ -125,12 +124,10 @@ export default function AppStateProvider({
|
|||||||
setUserEmailEager(getAuthEmailCookie());
|
setUserEmailEager(getAuthEmailCookie());
|
||||||
}, []);
|
}, []);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authError) {
|
if (auth === null || authError) {
|
||||||
setUserEmail(undefined);
|
setUserEmail(undefined);
|
||||||
setUserEmailEager(undefined);
|
setUserEmailEager(undefined);
|
||||||
if (isCredentialsSignInError(authError)) {
|
clearAuthEmailCookie();
|
||||||
clearAuthEmailCookie();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
setUserEmail(auth?.user?.email ?? undefined);
|
setUserEmail(auth?.user?.email ?? undefined);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user