Clear cookie when server rejects auth check

This commit is contained in:
Sam Becker 2025-04-22 17:06:40 -05:00
parent 56aaf14c7c
commit afa34f9b03
2 changed files with 7 additions and 0 deletions

View File

@ -19,6 +19,9 @@ export const clearAuthEmailCookie = () =>
export const hasAuthEmailCookie = () =>
Boolean(getCookie(KEY_AUTH_EMAIL));
export const isCredentialsSignInError = (error: any) =>
(error.message || `${error}`).includes(KEY_CREDENTIALS_SIGN_IN_ERROR);
export const generateAuthSecret = () => fetch(
'https://generate-secret.vercel.app/32',
{ cache: 'no-cache' },

View File

@ -19,6 +19,7 @@ import {
storeAuthEmailCookie,
clearAuthEmailCookie,
hasAuthEmailCookie,
isCredentialsSignInError,
} from '@/auth';
import { useRouter, usePathname } from 'next/navigation';
import { isPathAdmin, PATH_ROOT } from '@/app/paths';
@ -126,6 +127,9 @@ export default function AppStateProvider({
if (authError) {
setIsUserSignedInEager(false);
setUserEmail(undefined);
if (isCredentialsSignInError(authError)) {
clearAuthEmailCookie();
}
} else {
setUserEmail(auth?.user?.email ?? undefined);
}