File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import React from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22import { BrowserRouter , Route , Routes , Navigate } from 'react-router-dom' ;
33
44import Home from '@pages/Home' ;
@@ -28,9 +28,21 @@ import ChatRoom from '@pages/Chats/ChatRoom';
2828import MatchingRoom from '@pages/Chats/MatchingRoom' ;
2929
3030import NotFound from '@pages/NotFound' ;
31+ import { getUserInfoApi } from '@apis/user' ;
32+ import { getCurrentUserId } from '@utils/getCurrentUserId' ;
3133
3234const ProtectedRoute = ( { children } : { children : JSX . Element } ) => {
33- const isAuthenticated = Boolean ( localStorage . getItem ( 'new_jwt_token' ) ) ;
35+ const [ isAuthenticated , setIsAuthenticated ] = useState < boolean | null > ( null ) ;
36+
37+ useEffect ( ( ) => {
38+ const checkAuth = async ( ) => {
39+ const currentUserId = getCurrentUserId ( ) ;
40+ const response = await getUserInfoApi ( currentUserId ) ;
41+ setIsAuthenticated ( response . isSuccess ) ;
42+ } ;
43+ checkAuth ( ) ;
44+ } , [ ] ) ;
45+
3446 return isAuthenticated ? children : < Navigate to = "/login" /> ;
3547} ;
3648
You can’t perform that action at this time.
0 commit comments