refactor: Centralize logout logic to a dedicated page, clear local storage, and prevent API requests during the process.

This commit is contained in:
pradeepkumar
2026-03-08 01:55:46 +05:30
parent b45ceef4e9
commit c28f1a71ea
3 changed files with 31 additions and 5 deletions

View File

@@ -1,17 +1,33 @@
'use client';
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { signOut } from 'next-auth/react';
export default function LogoutPage() {
const hasStarted = useRef(false);
useEffect(() => {
if (hasStarted.current) return;
hasStarted.current = true;
// Clear localStorage tokens
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
localStorage.removeItem('user');
// Sign out via NextAuth (clears session cookie) and redirect to login
signOut({ callbackUrl: '/login', redirect: true });
// Sign out via NextAuth (clears session cookie), then manually redirect
signOut({ redirect: false })
.then(() => {
window.location.href = '/login';
})
.catch(() => {
window.location.href = '/login';
});
// Safety fallback - if signOut hangs, redirect after 3 seconds
setTimeout(() => {
window.location.href = '/login';
}, 3000);
}, []);
return (