aboutsummaryrefslogtreecommitdiff
path: root/web/src/components/auth/ProtectedRoute.tsx
blob: 1133d36736130719771a9902fb8536f82b792146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * ProtectedRoute.tsx
 * Redireciona para /login se o usuário não estiver autenticado.
 */

import { Navigate, useLocation } from 'react-router-dom';
import { useAuth } from '@/context/AuthContext';
import type { ReactNode } from 'react';

export function ProtectedRoute({ children }: { children: ReactNode }) {
  const { isAuthenticated } = useAuth();
  const location = useLocation();

  if (!isAuthenticated) {
    // Preserva a rota que o usuário tentou acessar para redirect pós-login
    return <Navigate to="/login" state={{ from: location }} replace />;
  }

  return <>{children}</>;
}