session correction logout 3 rest

This commit is contained in:
alma 2025-04-18 13:59:30 +02:00
parent 50cf9ca06b
commit 0f3818bb99
3 changed files with 46 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import NextAuth, { NextAuthOptions } from "next-auth";
import KeycloakProvider from "next-auth/providers/keycloak";
import { prisma } from '@/lib/prisma';
import { ExtendedJWT, ExtendedSession, ServiceToken, invalidateServiceTokens } from '@/lib/session';
import { ExtendedJWT, ExtendedSession, ServiceToken, invalidateServiceTokens, clearAllCookies } from '@/lib/session';
import { Session } from "next-auth";
declare module "next-auth" {
@ -55,7 +55,7 @@ export const authOptions: NextAuthOptions = {
],
session: {
strategy: "jwt",
maxAge: 24 * 60 * 60, // 1 day
maxAge: 8 * 60 * 60, // 8 hours
},
cookies: {
sessionToken: {
@ -67,7 +67,7 @@ export const authOptions: NextAuthOptions = {
sameSite: 'lax',
path: '/',
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 // 1 day
maxAge: 8 * 60 * 60 // 8 hours
}
},
callbackUrl: {
@ -79,7 +79,7 @@ export const authOptions: NextAuthOptions = {
sameSite: 'lax',
path: '/',
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 // 1 day
maxAge: 8 * 60 * 60 // 8 hours
}
},
csrfToken: {
@ -91,7 +91,7 @@ export const authOptions: NextAuthOptions = {
sameSite: 'lax',
path: '/',
secure: process.env.NODE_ENV === 'production',
maxAge: 24 * 60 * 60 // 1 day
maxAge: 8 * 60 * 60 // 8 hours
}
}
},
@ -177,14 +177,20 @@ export const authOptions: NextAuthOptions = {
accessToken: extendedToken.accessToken ?? '',
refreshToken: extendedToken.refreshToken,
serviceTokens: extendedToken.serviceTokens ?? {},
expires: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(),
expires: new Date(Date.now()).toISOString(), // Expire immediately
} as ExtendedSession);
// Force clear all cookies on signout
if (typeof window !== 'undefined') {
clearAllCookies();
}
}
}
},
pages: {
signIn: '/signin',
error: '/signin',
signOut: '/signin', // Redirect to signin after signout
},
debug: process.env.NODE_ENV === 'development',
};

10
app/signout/page.tsx Normal file
View File

@ -0,0 +1,10 @@
import { SignOutHandler } from "@/components/auth/signout-handler";
export default function SignOut() {
return (
<div className="min-h-screen flex items-center justify-center">
<SignOutHandler />
<p>Déconnexion en cours...</p>
</div>
);
}

View File

@ -0,0 +1,24 @@
"use client";
import { useEffect } from "react";
import { signOut } from "next-auth/react";
import { clearAllCookies } from "@/lib/session";
export function SignOutHandler() {
useEffect(() => {
const handleSignOut = async () => {
// Clear all cookies first
clearAllCookies();
// Then sign out from NextAuth
await signOut({
callbackUrl: "/signin",
redirect: true
});
};
handleSignOut();
}, []);
return null;
}