fix(verifayda): format nested Fayda address objects into strings

This commit is contained in:
ghost2023
2026-08-02 00:11:39 +03:00
parent 6af63717ea
commit aaf0aa3c92
6 changed files with 173 additions and 8 deletions

View File

@@ -72,6 +72,20 @@ export default function FaydaVerifyPanel({
// panel between steps can't complete a verification against the wrong person.
const subjectRef = useRef(subject);
subjectRef.current = subject;
// FaydaCallbackPage posts its message from a StrictMode-double-invoked
// effect in dev, so the same one-time-use code+state can arrive twice.
// Track the last state we've started completing so the resend is a no-op.
const handledStateRef = useRef<string | null>(null);
// Polls the popup so a manually-closed window (no postMessage ever sent)
// still clears `loading` instead of leaving the button spinning forever.
const pollRef = useRef<number | null>(null);
const stopPolling = () => {
if (pollRef.current !== null) {
window.clearInterval(pollRef.current);
pollRef.current = null;
}
};
useEffect(() => {
const onMessage = async (event: MessageEvent<FaydaCallbackMessage>) => {
@@ -79,11 +93,15 @@ export default function FaydaVerifyPanel({
if (event.data?.type !== "fayda-callback") return;
if (event.data.error) {
stopPolling();
setLoading(false);
setError(event.data.errorDescription ?? event.data.error);
return;
}
if (!event.data.code || !event.data.state) return;
if (handledStateRef.current === event.data.state) return;
handledStateRef.current = event.data.state;
stopPolling();
try {
const next = await verifaydaService.completeIdentity(
@@ -104,13 +122,17 @@ export default function FaydaVerifyPanel({
}
};
window.addEventListener("message", onMessage);
return () => window.removeEventListener("message", onMessage);
return () => {
window.removeEventListener("message", onMessage);
stopPolling();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const startVerification = async () => {
setError(null);
setLoading(true);
handledStateRef.current = null;
try {
const authorizationUrl = await verifaydaService.start();
const popup = window.open(
@@ -121,8 +143,17 @@ export default function FaydaVerifyPanel({
if (!popup) {
setLoading(false);
setError("Pop-up blocked — allow pop-ups for this site and try again.");
return;
}
// Loading stays on until the popup posts back.
// Loading stays on until the popup posts back — unless the user closes
// it by hand, which never sends a message; poll for that and clear
// loading ourselves so the button doesn't spin forever.
stopPolling();
pollRef.current = window.setInterval(() => {
if (!popup.closed) return;
stopPolling();
if (handledStateRef.current === null) setLoading(false);
}, 500);
} catch (err) {
setLoading(false);
setError(