/ src / auth / IdleGuard.tsx
IdleGuard.tsx
 1  // Copyright (c) 2026 VPL Solutions. All rights reserved.
 2  // Licensed under the MIT License. See LICENSE for details.
 3  
 4  import { type ReactNode } from 'react';
 5  import { useNavigate, useLocation } from 'react-router-dom';
 6  import { useAuth } from './useAuth';
 7  import { useIdleTimer } from '../hooks/useIdleTimer';
 8  
 9  /** 15 minutes of inactivity before redirecting to the Standby page. */
10  const IDLE_TIMEOUT_MS = 15 * 60 * 1000;
11  
12  /**
13   * Wraps authenticated routes. After IDLE_TIMEOUT_MS of user inactivity,
14   * redirects to /standby carrying the current path so the session can resume.
15   * Only active when auth is enabled and the user is authenticated.
16   */
17  export function IdleGuard({ children }: { children: ReactNode }) {
18    const { authEnabled, isAuthenticated } = useAuth();
19    const navigate = useNavigate();
20    const location = useLocation();
21  
22    useIdleTimer({
23      timeout: IDLE_TIMEOUT_MS,
24      enabled: authEnabled && isAuthenticated,
25      onIdle: () => {
26        navigate('/standby', { state: { returnTo: location.pathname } });
27      },
28    });
29  
30    return <>{children}</>;
31  }