diff --git a/src/routes/userRoute.tsx b/src/routes/userRoute.tsx index 2ba95c4d..ba7da636 100644 --- a/src/routes/userRoute.tsx +++ b/src/routes/userRoute.tsx @@ -8,29 +8,38 @@ interface UserRouteProps { } const UserRoute: React.FC = ({ children }) => { - const [ok, setOk] = useState(true); + const [ok, setOk] = useState(null); // Use null to indicate loading state const navigate = useNavigate(); - const { notify } = useGlobalInfoStore(); + useEffect(() => { fetchUser(); }, []); const fetchUser = async () => { try { - const { data } = await axios.get('/api/current-user'); + const { data } = await axios.get('http://localhost:8080/auth/current-user'); if (data.ok) { setOk(true); + } else { + setOk(false); + notify('error', data.error || 'Please login again to continue'); + navigate('/login'); } } catch (err: any) { setOk(false); - notify('error', err.message || 'Please login again to continue'); - navigate('/'); + notify('error', err.response?.data?.error || 'An error occurred. Please login again.'); + navigate('/login'); } }; - // Display loading message while fetching user data - return
{!ok ?

Loading...

: <>{children}}
; + // Loading state + if (ok === null) { + return

Loading...

; + } + + // Render children if authenticated + return <>{ok ? children : null}; }; export default UserRoute;