File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
frontend/src/components/ProtectedRoute Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change 1- import { ReactNode } from 'react' ;
1+ import { ReactNode , useEffect } from 'react' ;
22import { Navigate , useLocation } from 'react-router-dom' ;
3- import { useSelector } from 'react-redux' ;
3+ import { useSelector , useDispatch } from 'react-redux' ;
44import { RootState } from '../../services/actions/types' ;
5+ import { AppDispatch , checkAuthenticated } from '../../services/actions/auth' ;
56import Spinner from '../LoadingSpinner/LoadingSpinner' ;
67
78interface ProtectedRouteProps {
@@ -10,8 +11,16 @@ interface ProtectedRouteProps {
1011
1112const ProtectedRoute = ( { children } : ProtectedRouteProps ) => {
1213 const location = useLocation ( ) ;
14+ const dispatch = useDispatch < AppDispatch > ( ) ;
1315 const { isAuthenticated } = useSelector ( ( state : RootState ) => state . auth ) ;
14-
16+
17+ // Check authentication status when component mounts
18+ useEffect ( ( ) => {
19+ if ( isAuthenticated === null ) {
20+ dispatch ( checkAuthenticated ( ) ) ;
21+ }
22+ } , [ dispatch , isAuthenticated ] ) ;
23+
1524 // Wait for auth check to complete (null means not checked yet)
1625 // TODO: Consider adding error handling for auth check failures
1726 if ( isAuthenticated === null ) {
You can’t perform that action at this time.
0 commit comments