Fix redirect loop on session timeout
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -15,18 +14,22 @@ const AUTO_REDIRECT_SECONDS = 5;
|
||||
export default function SessionTimeoutModal() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [secondsLeft, setSecondsLeft] = useState(AUTO_REDIRECT_SECONDS);
|
||||
const navigate = useNavigate();
|
||||
const timerRef = useRef<ReturnType<typeof setInterval>>();
|
||||
const wasOpenRef = useRef(false);
|
||||
|
||||
const redirectToLogin = useCallback(() => {
|
||||
tokenManager.clearToken();
|
||||
navigate('/login', { replace: true });
|
||||
}, [navigate]);
|
||||
window.location.href = '/login';
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const onSessionExpired = () => {
|
||||
setOpen(true);
|
||||
setSecondsLeft(AUTO_REDIRECT_SECONDS);
|
||||
// Only reset the countdown when the modal transitions from closed → open
|
||||
if (!wasOpenRef.current) {
|
||||
setSecondsLeft(AUTO_REDIRECT_SECONDS);
|
||||
}
|
||||
wasOpenRef.current = true;
|
||||
};
|
||||
window.addEventListener('session:expired', onSessionExpired);
|
||||
return () => window.removeEventListener('session:expired', onSessionExpired);
|
||||
|
||||
Reference in New Issue
Block a user