UNPKG

@sqala/threedsecure-react

Version:

A React library for implementing 3D Secure authentication flows in payment applications

67 lines (66 loc) 2.54 kB
import { AuthenticationState as u } from "../types/authentication.js"; import { Bucket as l } from "../models/bucket.js"; import { useCallback as w } from "react"; const b = ({ baseUrl: i = "https://api.sqala.tech/core/v1/threedsecure", publicKey: c }) => { const A = w( (s, r, t) => { const a = new EventSource(`${i}/${s.id}/listen?publicKey=${c}`), n = new l(), p = () => { try { n.close(), a.close(); } catch (o) { t("useApi: executeAuthentication - close - error", o); } }; return a.onmessage = (o) => { try { const e = JSON.parse(o.data); t("useApi: executeAuthentication - onmessage", e), n.push(e), (e.state === u.Failed || e.state === u.AuthorizedToAttempt || e.state === u.Completed || r.aborted) && p(); } catch (e) { t("useApi: executeAuthentication - onmessage - error", e); } }, a.onerror = (o) => { t("useApi: executeAuthentication - onerror", o), r.aborted && p(); }, r.addEventListener("abort", () => { t("useApi: executeAuthentication - abort"), p(); }), n.iterator; }, [i, c] ); return { setBrowserData: w( async (s, r, t) => { const a = async () => (await (await fetch("https://api.ipify.org?format=json")).json()).ip, n = s.ip ?? await a(); t("useApi: setBrowserData", s); const o = [48, 32, 24, 16, 15, 8, 4, 1].find((h) => h <= screen.colorDepth) ?? 48, e = { ip: n, javaEnabled: !0, javascriptEnabled: !0, language: navigator.language, userAgent: navigator.userAgent, screenWidth: window.screen.width, screenHeight: window.screen.height, timeZoneOffset: (/* @__PURE__ */ new Date()).getTimezoneOffset(), colorDepth: o, acceptHeader: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" }; t("useApi: setBrowserData - browser", e); const d = await fetch(`${i}/${s.id}/browser?publicKey=${c}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(e), signal: r }); if (t("useApi: setBrowserData - response", d), !d.ok) throw new Error("Failed to set browser data"); }, [i, c] ), executeAuthentication: A }; }; export { b as useApi }; //# sourceMappingURL=useApi.js.map