()
| 55 | // ============================================================================ |
| 56 | |
| 57 | export function useSessionManager() { |
| 58 | const { isSpeedMode, isDeploymentReady } = useDeployment(); |
| 59 | const reconcileExpiryRef = useRef<() => void>(() => {}); |
| 60 | |
| 61 | useEffect(() => { |
| 62 | if (isSpeedMode || !isDeploymentReady) return; |
| 63 | |
| 64 | if (!hasAuthCookies()) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (checkSessionValid()) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | handleSessionExpired(); |
| 73 | }, [isSpeedMode, isDeploymentReady]); |
| 74 | |
| 75 | /** |
| 76 | * Proactive session expiry watcher |
| 77 | * Triggers session-expired even if user does not make any API request |
| 78 | */ |
| 79 | useEffect(() => { |
| 80 | if (isSpeedMode || !isDeploymentReady) return; |
| 81 | |
| 82 | let timeoutId: number | null = null; |
| 83 | let intervalId: number | null = null; |
| 84 | |
| 85 | const clearTimers = () => { |
| 86 | if (timeoutId !== null) { |
| 87 | window.clearTimeout(timeoutId); |
| 88 | timeoutId = null; |
| 89 | } |
| 90 | if (intervalId !== null) { |
| 91 | window.clearInterval(intervalId); |
| 92 | intervalId = null; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | const scheduleExpiryCheck = () => { |
| 97 | clearTimers(); |
| 98 | |
| 99 | const expiresAt = getTokenExpiresAt(); |
| 100 | if (expiresAt === null) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | const now = Date.now(); |
| 105 | const delayMs = expiresAt * 1000 - now; |
| 106 | |
| 107 | if (delayMs <= 0) { |
| 108 | handleSessionExpired(); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | timeoutId = window.setTimeout(() => { |
| 113 | if (!checkSessionValid()) { |
| 114 | handleSessionExpired(); |
no test coverage detected