Skip to content

Commit

Permalink
Fixed compliance with login persistence on Next Auth cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Dec 19, 2023
1 parent 4c8015a commit de038bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
21 changes: 20 additions & 1 deletion app/[locale]/authentication/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import prisma from "@/utilities/prisma";
import schema from "@/schemas/authentication";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { AuthError } from "next-auth";
import { auth, signIn, signOut } from "@/utilities/next-auth";
Expand Down Expand Up @@ -139,7 +140,8 @@ export async function signInAccount(
// d'authentification fournies par l'utilisateur.
const result = schema.safeParse( {
email: formData.get( "email" ),
password: formData.get( "password" )
password: formData.get( "password" ),
remembered: formData.get( "remembered" ) === "on"
} );

if ( !result.success )
Expand Down Expand Up @@ -201,6 +203,23 @@ export async function signInAccount(

if ( response )
{
// Lorsqu'une réponse semble avoir été récupérée précédemment,
// on tente alors de mettre à jour la durée de vie du cookie
// d'authentification de l'utilisateur avant de le rediriger
// vers la page de la réponse.
// Source : https://github.com/nextauthjs/next-auth/discussions/3794
const cookiesList = cookies();
const authCookie = cookiesList.get( "authjs.session-token" );

if ( authCookie )
{
cookiesList.set( {
// https://github.com/nextauthjs/next-auth/blob/065b7e9d9b8d046758e381c88ef351e65764ea5f/packages/core/src/index.ts#L238-L243
...authCookie,
maxAge: 24 * 60 * 60 * ( result.data.remembered ? 30 : 1 )
} );
}

redirect( response );
}

Expand Down
9 changes: 6 additions & 3 deletions app/[locale]/components/authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export default function Authentification()
<FormField
name="remembered"
control={form.control}
render={() => (
render={( { field } ) => (
<FormItem>
<FormLabel className="sr-only">
Se souvenir de moi
Expand All @@ -345,11 +345,14 @@ export default function Authentification()
<FormControl>
<div className="flex items-center justify-center space-x-2">
<Switch
id="remember-me"
id="remembered"
name="remembered"
checked={field.value}
disabled={loading}
onCheckedChange={field.onChange}
/>

<Label htmlFor="remember-me">
<Label htmlFor="remembered">
Se souvenir de moi
</Label>
</div>
Expand Down
2 changes: 1 addition & 1 deletion schemas/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const schema = z.object( {
password: z.string().min( 10 ).max( 60 ).or( z.literal( "" ) ),

// Se souvenir de moi.
remembered: z.boolean().optional()
remembered: z.boolean()
} );

export default schema;

0 comments on commit de038bf

Please sign in to comment.