fix: robust logout session clearing with cookie cleanup and isLoggingOut guard

- Clear next-auth session cookies directly as backup in logout page
- Add isLoggingOut localStorage flag to prevent TokenSync/PresenceProvider from restoring tokens
- Clear isLoggingOut flag on login page load as safety net
- Add createdAt to AgentSubscription interface (fixes build error)
- Add /education to public routes in middleware
This commit is contained in:
pradeepkumar
2026-03-08 12:06:07 +05:30
parent 8c499ecc6a
commit b49b07fdd6
10 changed files with 64 additions and 17 deletions

View File

@@ -3,6 +3,25 @@
import { useEffect, useRef } from 'react';
import { signOut } from 'next-auth/react';
function clearAuthCookies() {
// Clear next-auth v5 (Auth.js) and v4 session cookies directly
const cookieNames = [
'authjs.session-token',
'__Secure-authjs.session-token',
'authjs.callback-url',
'authjs.csrf-token',
'next-auth.session-token',
'__Secure-next-auth.session-token',
'next-auth.callback-url',
'next-auth.csrf-token',
];
cookieNames.forEach((name) => {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${window.location.hostname}`;
});
}
export default function LogoutPage() {
const hasStarted = useRef(false);
@@ -10,24 +29,31 @@ export default function LogoutPage() {
if (hasStarted.current) return;
hasStarted.current = true;
// Ensure logout flag is set (prevents TokenSync and PresenceProvider from restoring tokens)
localStorage.setItem('isLoggingOut', 'true');
// Clear localStorage tokens
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('user');
// Clear auth cookies directly as a robust backup
clearAuthCookies();
const redirectToLogin = () => {
clearAuthCookies();
// Clear the logout flag right before redirecting
localStorage.removeItem('isLoggingOut');
window.location.href = '/login';
};
// Sign out via NextAuth (clears session cookie), then manually redirect
signOut({ redirect: false })
.then(() => {
window.location.href = '/login';
})
.catch(() => {
window.location.href = '/login';
});
.then(redirectToLogin)
.catch(redirectToLogin);
// Safety fallback - if signOut hangs, redirect after 3 seconds
setTimeout(() => {
window.location.href = '/login';
}, 3000);
setTimeout(redirectToLogin, 3000);
}, []);
return (