-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathProtectedRoute.tsx
More file actions
38 lines (33 loc) · 1.15 KB
/
ProtectedRoute.tsx
File metadata and controls
38 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { ReactNode, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { AUTH_FAILURE_REASONS } from "@web/common/constants/auth.constants";
import { ROOT_ROUTES } from "@web/common/constants/routes";
import { AbsoluteOverflowLoader } from "@web/components/AbsoluteOverflowLoader";
import { useAuthCheck } from "./useAuthCheck";
export const ProtectedRoute = ({ children }: { children: ReactNode }) => {
const navigate = useNavigate();
const { isAuthenticated, isCheckingAuth, isGoogleTokenActive } =
useAuthCheck();
useEffect(() => {
const handleAuthCheck = () => {
if (isAuthenticated === false) {
if (isGoogleTokenActive === false) {
navigate(
`${ROOT_ROUTES.LOGIN}?reason=${AUTH_FAILURE_REASONS.GAUTH_SESSION_EXPIRED}`,
);
} else {
navigate(
`${ROOT_ROUTES.LOGIN}?reason=${AUTH_FAILURE_REASONS.USER_SESSION_EXPIRED}`,
);
}
}
};
void handleAuthCheck();
}, [isAuthenticated, isGoogleTokenActive, navigate]);
return (
<>
{isCheckingAuth && <AbsoluteOverflowLoader />}
{children}
</>
);
};