Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions frontend/src/components/ProtectedRoute/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ReactNode } from 'react';
import { ReactNode, useEffect } from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { useSelector, useDispatch } from 'react-redux';
import { RootState } from '../../services/actions/types';
import { AppDispatch, checkAuthenticated } from '../../services/actions/auth';
import Spinner from '../LoadingSpinner/LoadingSpinner';

interface ProtectedRouteProps {
Expand All @@ -10,8 +11,16 @@ interface ProtectedRouteProps {

const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
const location = useLocation();
const dispatch = useDispatch<AppDispatch>();
const { isAuthenticated } = useSelector((state: RootState) => state.auth);


// Check authentication status when component mounts
useEffect(() => {
if (isAuthenticated === null) {
dispatch(checkAuthenticated());
}
}, [dispatch, isAuthenticated]);

// Wait for auth check to complete (null means not checked yet)
// TODO: Consider adding error handling for auth check failures
if (isAuthenticated === null) {
Expand Down