Refine admin auth handling

This commit is contained in:
Sam Becker 2025-05-22 09:21:03 -05:00
parent a79ec4f078
commit 14ccc20f7a
2 changed files with 4 additions and 7 deletions

View File

@ -22,8 +22,8 @@ export const hasAuthEmailCookie = () =>
export const clearAuthEmailCookie = () =>
deleteCookie(KEY_AUTH_EMAIL);
export const isCredentialsSignInError = (error: any) =>
(error.message || `${error}`).includes(KEY_CREDENTIALS_SIGN_IN_ERROR);
export const isCredentialsSignInError = (error?: any) =>
(error?.message || `${error}`).includes(KEY_CREDENTIALS_SIGN_IN_ERROR);
export const generateAuthSecret = () => fetch(
'https://generate-secret.vercel.app/32',

View File

@ -18,7 +18,6 @@ import { AdminData, getAdminDataAction } from '@/admin/actions';
import {
storeAuthEmailCookie,
clearAuthEmailCookie,
isCredentialsSignInError,
getAuthEmailCookie,
} from '@/auth';
import { useRouter, usePathname } from 'next/navigation';
@ -125,12 +124,10 @@ export default function AppStateProvider({
setUserEmailEager(getAuthEmailCookie());
}, []);
useEffect(() => {
if (authError) {
if (auth === null || authError) {
setUserEmail(undefined);
setUserEmailEager(undefined);
if (isCredentialsSignInError(authError)) {
clearAuthEmailCookie();
}
} else {
setUserEmail(auth?.user?.email ?? undefined);
}