feat: implement robust logout API route to clear all authentication cookies

This commit is contained in:
pradeepkumar
2026-04-08 11:29:03 +05:30
parent c765ea9548
commit 92db35c9e8
2 changed files with 60 additions and 3 deletions

View File

@@ -33,15 +33,27 @@ export default function LogoutPage() {
localStorage.removeItem('refreshToken');
localStorage.removeItem('user');
// Call NextAuth signOut — canonical way to clear session cookie
// (uses the exact cookie name/attributes from cookies config in auth.ts)
// Call NextAuth signOut first (clears session via NextAuth's own logic)
try {
await signOut({ redirect: false });
} catch {
// Continue even if signOut fails
}
// Belt-and-suspenders: server action to nuke any remaining auth cookies
// Hit our explicit logout route handler — returns Set-Cookie headers
// that nuke ALL auth cookies. Route handlers are more reliable than
// server actions for cookie deletion in production.
try {
await fetch('/api/logout', {
method: 'POST',
credentials: 'include',
cache: 'no-store',
});
} catch {
// Continue
}
// Server action fallback
try {
await clearAuthCookies();
} catch {