refactor: Centralize logout logic to a dedicated page, clear local storage, and prevent API requests during the process.
This commit is contained in:
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user