fix: Enhance logout cookie deletion to cover more NextAuth cookie variants and adjust the logout sequence, and disable prefetching for footer links.

This commit is contained in:
pradeepkumar
2026-03-16 17:49:09 +05:30
parent 7d7566f6d3
commit fe0ef8cfba
3 changed files with 44 additions and 7 deletions

View File

@@ -6,6 +6,7 @@ export async function clearAuthCookies() {
const cookieStore = await cookies();
// Delete all next-auth v5 cookie variants
// Must specify path and secure options to match how they were set
const cookieNames = [
'authjs.session-token',
'authjs.csrf-token',
@@ -13,10 +14,36 @@ export async function clearAuthCookies() {
'__Secure-authjs.session-token',
'__Secure-authjs.csrf-token',
'__Secure-authjs.callback-url',
// next-auth v4 fallback names
'next-auth.session-token',
'next-auth.csrf-token',
'next-auth.callback-url',
'__Secure-next-auth.session-token',
'__Secure-next-auth.csrf-token',
'__Secure-next-auth.callback-url',
// Host-prefixed variants (used when behind a proxy)
'__Host-authjs.csrf-token',
'__Host-next-auth.csrf-token',
];
for (const name of cookieNames) {
cookieStore.delete(name);
// Delete with explicit options to ensure cookie is actually removed
cookieStore.delete({
name,
path: '/',
});
}
// Also try getting all cookies and deleting any auth-related ones
const allCookies = cookieStore.getAll();
for (const cookie of allCookies) {
if (
cookie.name.includes('authjs') ||
cookie.name.includes('next-auth') ||
cookie.name.includes('session-token')
) {
cookieStore.delete({ name: cookie.name, path: '/' });
}
}
return { success: true };

View File

@@ -25,8 +25,14 @@ export default function LogoutPage() {
localStorage.removeItem('refreshToken');
localStorage.removeItem('user');
// Delete httpOnly cookies via server action FIRST
// This ensures the session cookie is gone before signOut triggers session checks
await clearAuthCookies();
// Client-side signOut to clear NextAuth client state
await signOut({ redirect: false });
// Delete httpOnly cookies via server action (Next.js cookies() API)
// Delete cookies again after signOut in case signOut recreated any
await clearAuthCookies();
// Redirect to login immediately

View File

@@ -98,9 +98,10 @@ export function Footer() {
</h3>
<ul className="space-y-3">
{footerLinks.services.links.map((link) => (
<li key={link.href}>
<li key={link.label}>
<Link
href={link.href}
prefetch={false}
className="font-serif font-normal text-[14px] leading-[19px] text-[#00293D] hover:text-[#e58625] transition-colors"
>
{link.label}
@@ -117,9 +118,10 @@ export function Footer() {
</h3>
<ul className="space-y-3">
{footerLinks.serviceProviders.links.map((link) => (
<li key={link.href}>
<li key={link.label}>
<Link
href={link.href}
prefetch={false}
className="font-serif font-normal text-[14px] leading-[19px] text-[#00293D] hover:text-[#e58625] transition-colors"
>
{link.label}
@@ -136,9 +138,10 @@ export function Footer() {
</h3>
<ul className="space-y-3">
{footerLinks.resources.links.map((link) => (
<li key={link.href}>
<li key={link.label}>
<Link
href={link.href}
prefetch={false}
className="font-serif font-normal text-[14px] leading-[19px] text-[#00293D] hover:text-[#e58625] transition-colors"
>
{link.label}
@@ -155,9 +158,10 @@ export function Footer() {
</h3>
<ul className="space-y-3">
{footerLinks.aboutContact.links.map((link) => (
<li key={link.href}>
<li key={link.label}>
<Link
href={link.href}
prefetch={false}
className="font-serif font-normal text-[14px] leading-[19px] text-[#00293D] hover:text-[#e58625] transition-colors"
>
{link.label}
@@ -209,7 +213,7 @@ export function Footer() {
<div className="max-w-7xl mx-auto px-6 py-4">
<div className="flex flex-col md:flex-row items-center justify-between gap-4">
<p className="font-serif font-normal text-[15px] leading-[21px] text-[#00293D]">Copyright © 2025 Your Agency Name. All rights reserved.</p>
<Link href="/privacy-policy" className="font-serif font-normal text-[15px] leading-[21px] text-[#00293D] hover:text-[#e58625] transition-colors">
<Link href="/privacy-policy" prefetch={false} className="font-serif font-normal text-[15px] leading-[21px] text-[#00293D] hover:text-[#e58625] transition-colors">
Privacy Policy
</Link>
</div>