From 4c8015a2be178f5ddb20fd572aeac5f2be1d89dd Mon Sep 17 00:00:00 2001 From: Florian Trayon <26360935+FlorianLeChat@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:14:21 +0100 Subject: [PATCH] Added workaround for Next Auth redirects in try-catch blocks --- app/[locale]/authentication/actions.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/app/[locale]/authentication/actions.ts b/app/[locale]/authentication/actions.ts index c89159a..89ecf74 100644 --- a/app/[locale]/authentication/actions.ts +++ b/app/[locale]/authentication/actions.ts @@ -8,6 +8,7 @@ import prisma from "@/utilities/prisma"; import schema from "@/schemas/authentication"; import { redirect } from "next/navigation"; +import { AuthError } from "next-auth"; import { auth, signIn, signOut } from "@/utilities/next-auth"; // @@ -171,14 +172,17 @@ export async function signInAccount( }; } + let response; + try { // Dans le cas contraire, on tente alors une authentification via // les informations d'authentification fournies avant de rediriger // l'utilisateur vers la page de son tableau de bord. - await signIn( "credentials", { + response = await signIn( "credentials", { email: result.data.email, password: result.data.password, + redirect: false, // https://github.com/vercel/next.js/issues/55586 redirectTo: "/dashboard" } ); } @@ -186,10 +190,18 @@ export async function signInAccount( { // En cas d'erreur, on affiche un message d'erreur sur la page // d'authentification. - return { - success: false, - reason: "form.errors.invalid" - }; + if ( error instanceof AuthError ) + { + return { + success: false, + reason: "form.errors.generic" + }; + } + } + + if ( response ) + { + redirect( response ); } // On retourne enfin un message d'erreur par défaut au l'utilisateur