MCPcopy
hub / github.com/devias-io/material-kit-react / AuthGuard

Function AuthGuard

src/components/auth/auth-guard.tsx:15–55  ·  view source on GitHub ↗
({ children }: AuthGuardProps)

Source from the content-addressed store, hash-verified

13}
14
15export function AuthGuard({ children }: AuthGuardProps): React.JSX.Element | null {
16 const router = useRouter();
17 const { user, error, isLoading } = useUser();
18 const [isChecking, setIsChecking] = React.useState<boolean>(true);
19
20 const checkPermissions = async (): Promise<void> => {
21 if (isLoading) {
22 return;
23 }
24
25 if (error) {
26 setIsChecking(false);
27 return;
28 }
29
30 if (!user) {
31 logger.debug('[AuthGuard]: User is not logged in, redirecting to sign in');
32 router.replace(paths.auth.signIn);
33 return;
34 }
35
36 setIsChecking(false);
37 };
38
39 React.useEffect(() => {
40 checkPermissions().catch(() => {
41 // noop
42 });
43 // eslint-disable-next-line react-hooks/exhaustive-deps -- Expected
44 }, [user, error, isLoading]);
45
46 if (isChecking) {
47 return null;
48 }
49
50 if (error) {
51 return <Alert color="error">{error}</Alert>;
52 }
53
54 return <React.Fragment>{children}</React.Fragment>;
55}

Callers

nothing calls this directly

Calls 2

useUserFunction · 0.90
checkPermissionsFunction · 0.70

Tested by

no test coverage detected