Browse Source

feat: implement redirect handling in Intro component and update session storage in AuthBridge

master
sina_sajjadi 2 months ago
parent
commit
e3cfde4331
  1. 41
      src/app/intro/page.tsx
  2. 5
      src/lib/auth-bridge.ts

41
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) {
let isCancelled = false;
const redirectIfNeeded = async () => {
const shouldRedirect =
authBridge.isAuthenticated() &&
window.sessionStorage.getItem(REDIRECT_SESSION_KEY) === "true";
if (!shouldRedirect) {
if (!isCancelled) {
setIsCheckingRedirect(false);
}
return;
}
router.replace(localizePath(getSubmitPath(profile), locale));
}, [locale, profile, router]);
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 (
<div className="pt-7">
{isReportSheetOpen && (

5
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<string, unknown>) {
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" });

Loading…
Cancel
Save