From 23129adb9c74a7e9f28934aa71303f3d81d0a8a8 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sun, 14 Jan 2024 19:49:15 -0600 Subject: [PATCH] Make auth error catching more resilient --- src/auth/actions.ts | 11 +++++------ src/auth/index.ts | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/auth/actions.ts b/src/auth/actions.ts index b52c7f55..cb14b725 100644 --- a/src/auth/actions.ts +++ b/src/auth/actions.ts @@ -3,6 +3,7 @@ import { KEY_CALLBACK_URL, KEY_CREDENTIALS_SIGN_IN_ERROR, + KEY_CREDENTIALS_SIGN_IN_ERROR_URL, signIn, signOut, } from '@/auth'; @@ -16,19 +17,17 @@ export const signInAction = async ( try { await signIn('credentials', Object.fromEntries(formData)); } catch (error) { - console.log('signInAction error: string', `${error}`); - console.log('signInAction error: object', error); - if (`${error}`.includes(KEY_CREDENTIALS_SIGN_IN_ERROR)) { - console.log('signInAction: 01'); + if ( + `${error}`.includes(KEY_CREDENTIALS_SIGN_IN_ERROR) || + `${error}`.includes(KEY_CREDENTIALS_SIGN_IN_ERROR_URL) + ) { // Rethrow credentials error to display on the sign in page. return KEY_CREDENTIALS_SIGN_IN_ERROR; } else if (!`${error}`.includes('NEXT_REDIRECT')) { - console.log('signInAction: 02'); // Rethrow non-redirect errors throw error; } } - console.log('signInAction: 03'); redirect(formData.get(KEY_CALLBACK_URL) as string || PATH_ADMIN_PHOTOS); }; diff --git a/src/auth/index.ts b/src/auth/index.ts index b6057ed2..0bf0d476 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -3,6 +3,8 @@ import NextAuth, { User } from 'next-auth'; import Credentials from 'next-auth/providers/credentials'; export const KEY_CREDENTIALS_SIGN_IN_ERROR = 'CredentialsSignin'; +export const KEY_CREDENTIALS_SIGN_IN_ERROR_URL = + 'https://errors.authjs.dev#credentialssignin'; export const KEY_CALLBACK_URL = 'callbackUrl'; export const {