All files / src/components ProtectedRoute.tsx

100% Statements 22/22
100% Branches 6/6
100% Functions 1/1
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  1x 1x           4x 4x 4x   4x 1x 1x 1x 1x 1x 1x 1x   1x     4x 1x 1x     4x 1x 1x   1x 1x  
import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';
 
interface ProtectedRouteProps {
  children: React.ReactNode;
}
 
export default function ProtectedRoute({ children }: ProtectedRouteProps) {
  const { user, authEnabled, loading } = useAuth();
  const location = useLocation();
 
  if (loading) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-gray-50 dark:bg-gray-900">
        <div className="text-center">
          <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto mb-4"></div>
          <p className="text-gray-600 dark:text-gray-400">Loading...</p>
        </div>
      </div>
    );
  }
 
  // If auth is disabled, allow access
  if (!authEnabled) {
    return <>{children}</>;
  }
 
  // If auth is enabled but no user, redirect to login
  if (!user) {
    return <Navigate to="/login" state={{ from: location }} replace />;
  }
 
  return <>{children}</>;
}