|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import React, { useEffect } from "react"; |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | +import { useRouter, usePathname } from "next/navigation"; |
| 5 | +import { useMerchantHydrated, useMerchantSession } from "@/lib/merchant-store"; |
4 | 6 |
|
5 | 7 | export default function AuthGuard({ children }: { children: React.ReactNode }) { |
| 8 | + const hydrated = useMerchantHydrated(); |
| 9 | + const session = useMerchantSession(); |
| 10 | + const router = useRouter(); |
| 11 | + const pathname = usePathname(); |
| 12 | + const [mounted, setMounted] = useState(false); |
| 13 | + |
6 | 14 | useEffect(() => { |
7 | | - // Component mounted |
| 15 | + setMounted(true); |
8 | 16 | }, []); |
9 | 17 |
|
10 | | - // useEffect(() => { |
11 | | - // const isBypass = typeof window !== "undefined" && |
12 | | - // (window.location.search.includes("bypass=true") || process.env.NEXT_PUBLIC_DEV_BYPASS === "true"); |
13 | | - |
14 | | - // if (mounted && hydrated && !session && !isBypass) { |
15 | | - // router.push(`/login?callbackUrl=${encodeURIComponent(pathname)}`); |
16 | | - // } |
17 | | - // }, [mounted, hydrated, session, router, pathname]); |
18 | | - |
19 | | - // if (!mounted || !hydrated) { |
20 | | - // return null; |
21 | | - // } |
22 | | - |
23 | | - // const isBypass = typeof window !== "undefined" && |
24 | | - // (window.location.search.includes("bypass=true") || process.env.NEXT_PUBLIC_DEV_BYPASS === "true"); |
| 18 | + useEffect(() => { |
| 19 | + if (mounted && hydrated && !session) { |
| 20 | + router.push(`/login?callbackUrl=${encodeURIComponent(pathname)}`); |
| 21 | + } |
| 22 | + }, [mounted, hydrated, session, router, pathname]); |
25 | 23 |
|
26 | | - // if (!session && !isBypass) { |
27 | | - // return null; |
28 | | - // } |
| 24 | + if (!mounted || !hydrated) return null; |
| 25 | + if (!session) return null; |
29 | 26 |
|
30 | 27 | return <>{children}</>; |
31 | 28 | } |
0 commit comments