From f435537c6d4994b56b80fde053446298533de161 Mon Sep 17 00:00:00 2001 From: Michael-u21546551 Date: Tue, 22 Oct 2024 22:13:44 +0200 Subject: [PATCH] Refactor ProtectedRoutes component to include error handling for authentication check --- .../protectedRoutes/ProtectedRoutes.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/frontend/occupi-web/src/components/protectedRoutes/ProtectedRoutes.tsx b/frontend/occupi-web/src/components/protectedRoutes/ProtectedRoutes.tsx index 3b646100..a2758d3c 100644 --- a/frontend/occupi-web/src/components/protectedRoutes/ProtectedRoutes.tsx +++ b/frontend/occupi-web/src/components/protectedRoutes/ProtectedRoutes.tsx @@ -16,14 +16,18 @@ const ProtectedRoute = (props: ProtectedRouteProps) => { useEffect(() => { const checkAuthentication = async () => { - if(userDetails === null){ - const res = await AuthService.pingAdmin(); - if (res.status === 200) { - const userDeets = await AuthService.getUserDetails(); - setUserDetails(userDeets); - } else { - setUserDetails(null); + try{ + if(userDetails === null){ + const res = await AuthService.pingAdmin(); + if (res.status === 200) { + const userDeets = await AuthService.getUserDetails(); + setUserDetails(userDeets); + } else { + setUserDetails(null); + } } + } catch (error) { + setUserDetails(null); } setLoading(false); // Set loading to false after the check is done };