source: src/auth/guard/auth-guard.tsx@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import { useEffect, useCallback, useState } from 'react';
2// routes
3import { paths } from 'src/routes/paths';
4import { useRouter } from 'src/routes/hooks';
5//
6import { useAuthContext } from '../hooks';
7
8// ----------------------------------------------------------------------
9
10const loginPaths: Record<string, string> = {
11 firebase: paths.auth.firebase.login,
12};
13// ----------------------------------------------------------------------
14
15type Props = {
16 children: React.ReactNode;
17};
18
19export default function AuthGuard({ children }: Props) {
20 const router = useRouter();
21
22 const { authenticated, method } = useAuthContext();
23
24 const [checked, setChecked] = useState(false);
25
26 const check = useCallback(() => {
27 if (!authenticated) {
28 const searchParams = new URLSearchParams({
29 returnTo: window.location.pathname,
30 }).toString();
31
32 const loginPath = loginPaths[method];
33
34 const href = `${loginPath}?${searchParams}`;
35
36 router.replace(href);
37 } else {
38 setChecked(true);
39 }
40 }, [authenticated, method, router]);
41
42 useEffect(() => {
43 check();
44 // eslint-disable-next-line react-hooks/exhaustive-deps
45 }, []);
46
47 if (!checked) {
48 return null;
49 }
50
51 return <>{children}</>;
52}
Note: See TracBrowser for help on using the repository browser.