fix: Prevent redundant logout redirects and synchronously clear authentication tokens on the logout page.
This commit is contained in:
@@ -8,13 +8,19 @@ import Image from 'next/image';
|
|||||||
export default function LogoutPage() {
|
export default function LogoutPage() {
|
||||||
const hasStarted = useRef(false);
|
const hasStarted = useRef(false);
|
||||||
|
|
||||||
|
// Set logout flag synchronously on render to block API interceptor immediately
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
localStorage.setItem('isLoggingOut', 'true');
|
||||||
|
localStorage.removeItem('accessToken');
|
||||||
|
localStorage.removeItem('refreshToken');
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (hasStarted.current) return;
|
if (hasStarted.current) return;
|
||||||
hasStarted.current = true;
|
hasStarted.current = true;
|
||||||
|
|
||||||
const performLogout = async () => {
|
const performLogout = async () => {
|
||||||
// Clear localStorage
|
// Clear localStorage
|
||||||
localStorage.setItem('isLoggingOut', 'true');
|
|
||||||
localStorage.removeItem('accessToken');
|
localStorage.removeItem('accessToken');
|
||||||
localStorage.removeItem('refreshToken');
|
localStorage.removeItem('refreshToken');
|
||||||
localStorage.removeItem('user');
|
localStorage.removeItem('user');
|
||||||
|
|||||||
@@ -37,8 +37,11 @@ export const resetLogoutState = () => {
|
|||||||
// Force logout: redirect to /logout page which handles everything
|
// Force logout: redirect to /logout page which handles everything
|
||||||
const forceLogout = () => {
|
const forceLogout = () => {
|
||||||
if (isLoggingOut) return;
|
if (isLoggingOut) return;
|
||||||
isLoggingOut = true;
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
|
// Don't redirect to /logout if we're already on it or on /login
|
||||||
|
const path = window.location.pathname;
|
||||||
|
if (path === '/logout' || path === '/login') return;
|
||||||
|
isLoggingOut = true;
|
||||||
localStorage.setItem('isLoggingOut', 'true');
|
localStorage.setItem('isLoggingOut', 'true');
|
||||||
window.location.href = '/logout';
|
window.location.href = '/logout';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user