From e3cfde4331f6f0d11dd5cd467c816b58c8be613d Mon Sep 17 00:00:00 2001 From: sina_sajjadi Date: Sat, 16 May 2026 12:25:22 +0330 Subject: [PATCH] feat: implement redirect handling in Intro component and update session storage in AuthBridge --- src/app/intro/page.tsx | 45 +++++++++++++++++++++++++++++++++--------- src/lib/auth-bridge.ts | 5 +++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/app/intro/page.tsx b/src/app/intro/page.tsx index d37b896..bc5a9e9 100644 --- a/src/app/intro/page.tsx +++ b/src/app/intro/page.tsx @@ -12,6 +12,8 @@ import type { MarriageProfileResponse } from "@/hooks/marriage/types"; import { localizePath } from "@/i18n/config"; import { useI18n } from "@/i18n/provider"; +const REDIRECT_SESSION_KEY = "redirect"; + function getSubmitPath(profile: MarriageProfileResponse | undefined) { const isInCase = profile?.status === "in_case"; const isFemaleAcceptedFlow = @@ -39,18 +41,43 @@ export default function Intro() { const router = useRouter(); const { dictionary: t, locale } = useI18n(); const { data: profile, refetch } = useMarriageProfileQuery({ + enabled: false, retry: false, }); const [isReportSheetOpen, setIsReportSheetOpen] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); + const [isCheckingRedirect, setIsCheckingRedirect] = useState(true); useEffect(() => { - if (!profile) { - return; - } + let isCancelled = false; - router.replace(localizePath(getSubmitPath(profile), locale)); - }, [locale, profile, router]); + const redirectIfNeeded = async () => { + const shouldRedirect = + authBridge.isAuthenticated() && + window.sessionStorage.getItem(REDIRECT_SESSION_KEY) === "true"; + + if (!shouldRedirect) { + if (!isCancelled) { + setIsCheckingRedirect(false); + } + return; + } + + const profileResponse = profile ?? (await refetch()).data; + const nextPath = localizePath(getSubmitPath(profileResponse), locale); + window.sessionStorage.removeItem(REDIRECT_SESSION_KEY); + + if (!isCancelled) { + router.replace(nextPath); + } + }; + + void redirectIfNeeded(); + + return () => { + isCancelled = true; + }; + }, [locale, profile, refetch, router]); const handleSubmit = async () => { if (isSubmitting) { @@ -66,15 +93,15 @@ export default function Intro() { return; } } - - const profileResponse = profile ?? (await refetch()).data; - const nextPath = localizePath(getSubmitPath(profileResponse), locale); - router.push(nextPath); } finally { setIsSubmitting(false); } }; + if (isCheckingRedirect) { + return null; + } + return (
{isReportSheetOpen && ( diff --git a/src/lib/auth-bridge.ts b/src/lib/auth-bridge.ts index 07958a2..7b26331 100644 --- a/src/lib/auth-bridge.ts +++ b/src/lib/auth-bridge.ts @@ -2,6 +2,7 @@ import { getClientCookie } from "./cookies"; const TOKEN_COOKIE_NAME = "HABIB_TOKEN"; const COINS_COOKIE_NAME = "HABIB_COINS"; +const REDIRECT_SESSION_KEY = "redirect"; function postFlutterMessage(payload: Record) { if (typeof window === "undefined") { @@ -121,6 +122,10 @@ class AuthBridge { return true; } + if (typeof window !== "undefined") { + window.sessionStorage.setItem(REDIRECT_SESSION_KEY, "true"); + } + this.loginRequested = true; const sent = postFlutterMessage({ action: "login" });