-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthGuard.tsx
More file actions
47 lines (39 loc) · 1.55 KB
/
AuthGuard.tsx
File metadata and controls
47 lines (39 loc) · 1.55 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
39
40
41
42
43
44
45
46
47
"use client";
import { useEffect, useState } from "react";
import { PAGE_NAME } from "@ui/src/utils/constants/pageNames";
import { useRouter, usePathname } from "next/navigation";
import { useAuthStore } from "@/app/store/useAuthStore";
import { createWebViewEventListener } from "../../lib/bridge/createWebViewEventListener";
import { parseWebViewAuthMessage } from "../../lib/bridge/parseWebViewAuthMessage";
import { useDetectWebView } from "../_hooks/useDetectWebView";
import SignInForm from "./SignInForm";
export default function AuthGuard(): JSX.Element | null {
const router = useRouter();
const pathname = usePathname();
const [isLoading, setIsLoading] = useState(true);
const { isLoggedIn } = useAuthStore();
const { isIOSWebView, isAndroidWebView } = useDetectWebView();
const webViewEventListener = createWebViewEventListener({ isIOSWebView, isAndroidWebView });
useEffect(() => {
webViewEventListener(parseWebViewAuthMessage);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (!isLoggedIn) {
setIsLoading(false);
return;
}
if (pathname === "/" && typeof window !== "undefined") {
const urlParams = new URLSearchParams(window.location.search);
const prNumber = urlParams.get("pr");
if (prNumber) {
router.replace(`/dashboard?pr=${prNumber}`);
} else {
router.replace(PAGE_NAME.DASHBOARD);
}
}
setIsLoading(false);
}, [isLoggedIn, router, pathname]);
if (isLoading) return null;
return isLoggedIn ? null : <SignInForm />;
}