Skip to content

Commit

Permalink
Added workaround for Next Auth redirects in try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Dec 19, 2023
1 parent 842ae21 commit 4c8015a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/[locale]/authentication/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

//
Expand Down Expand Up @@ -171,25 +172,36 @@ 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"
} );
}
catch ( error )
{
// 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
Expand Down

0 comments on commit 4c8015a

Please sign in to comment.