feat(auth): implement Facebook identity binding on external entry
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
import {
|
||||
persistExternalEntryPayload,
|
||||
resolveExternalEntryPromotionType,
|
||||
resolveExternalEntryTarget,
|
||||
shouldBindExternalFacebookIdentity,
|
||||
} from "@/lib/navigation/external_entry";
|
||||
import {
|
||||
clearPendingChatPromotion,
|
||||
@@ -14,6 +15,7 @@ import {
|
||||
} from "@/lib/navigation/chat_unlock_session";
|
||||
import { ROUTES } from "@/router/routes";
|
||||
import { Logger } from "@/utils";
|
||||
import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context";
|
||||
|
||||
const log = new Logger("ExternalEntryPersist");
|
||||
|
||||
@@ -37,42 +39,87 @@ export default function ExternalEntryPersist({
|
||||
promotionType,
|
||||
}: ExternalEntryPersistProps) {
|
||||
const router = useRouter();
|
||||
const authState = useAuthState();
|
||||
const authDispatch = useAuthDispatch();
|
||||
const [hasPersisted, setHasPersisted] = useState(false);
|
||||
const hasNavigatedRef = useRef(false);
|
||||
const destination = resolveExternalEntryTarget({ target });
|
||||
const resolvedPromotionType = resolveExternalEntryPromotionType({
|
||||
mode,
|
||||
promotionType,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const destination = resolveExternalEntryTarget({ target });
|
||||
const resolvedPromotionType = resolveExternalEntryPromotionType({
|
||||
mode,
|
||||
promotionType,
|
||||
});
|
||||
let cancelled = false;
|
||||
|
||||
void (async () => {
|
||||
try {
|
||||
await Promise.all([
|
||||
persistExternalEntryPayload({
|
||||
deviceId,
|
||||
asid,
|
||||
psid,
|
||||
avatarUrl,
|
||||
}),
|
||||
destination === ROUTES.chat && resolvedPromotionType
|
||||
? savePendingChatPromotion(resolvedPromotionType)
|
||||
: clearPendingChatPromotion(),
|
||||
]);
|
||||
} catch (error) {
|
||||
log.warn("[ExternalEntryPersist] failed to persist payload", error);
|
||||
} finally {
|
||||
router.replace(destination);
|
||||
const results = await Promise.allSettled([
|
||||
persistExternalEntryPayload({
|
||||
deviceId,
|
||||
asid,
|
||||
psid,
|
||||
avatarUrl,
|
||||
}),
|
||||
destination === ROUTES.chat && resolvedPromotionType
|
||||
? savePendingChatPromotion(resolvedPromotionType)
|
||||
: clearPendingChatPromotion(),
|
||||
]);
|
||||
for (const result of results) {
|
||||
if (result.status === "rejected") {
|
||||
log.warn(
|
||||
"[ExternalEntryPersist] failed to persist payload",
|
||||
result.reason,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (!cancelled) setHasPersisted(true);
|
||||
})();
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [
|
||||
avatarUrl,
|
||||
asid,
|
||||
destination,
|
||||
deviceId,
|
||||
mode,
|
||||
promotionType,
|
||||
psid,
|
||||
resolvedPromotionType,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (hasNavigatedRef.current || !hasPersisted) return;
|
||||
if (!authState.hasInitialized || authState.isLoading) return;
|
||||
hasNavigatedRef.current = true;
|
||||
|
||||
if (
|
||||
shouldBindExternalFacebookIdentity({
|
||||
hasInitialized: authState.hasInitialized,
|
||||
isLoading: authState.isLoading,
|
||||
loginStatus: authState.loginStatus,
|
||||
asid,
|
||||
psid,
|
||||
})
|
||||
) {
|
||||
authDispatch({
|
||||
type: "AuthExternalFacebookIdentityBindRequested",
|
||||
asid: asid ?? undefined,
|
||||
psid: psid ?? undefined,
|
||||
});
|
||||
}
|
||||
router.replace(destination);
|
||||
}, [
|
||||
asid,
|
||||
authDispatch,
|
||||
authState.hasInitialized,
|
||||
authState.isLoading,
|
||||
authState.loginStatus,
|
||||
destination,
|
||||
hasPersisted,
|
||||
psid,
|
||||
router,
|
||||
target,
|
||||
]);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user