UNPKG

@0xsequence/connect

Version:
51 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EpicAuthProvider = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const design_system_1 = require("@0xsequence/design-system"); const react_1 = require("react"); const wagmi_1 = require("wagmi"); const EpicLogo_js_1 = require("../../connectors/epic/EpicLogo.js"); const localStorage_js_1 = require("../../constants/localStorage.js"); const useStorage_js_1 = require("../../hooks/useStorage.js"); // EpicAuthProvider handles Epic Games OAuth login redirects. // On mount, it checks the URL for Epic login results, stores the Epic JWT in storage, and triggers a connection with the Epic connector if found. const EpicAuthProvider = ({ children }) => { const { connectors, connect, isPending } = (0, wagmi_1.useConnect)(); const { isConnected } = (0, wagmi_1.useAccount)(); const storage = (0, useStorage_js_1.useStorage)(); const socialAuthConnectors = connectors .filter(c => c._wallet?.type === 'social') .filter(c => !c._wallet.id.includes('email')); // UseEffect to handle the redirect back from the worker for Epic login (0, react_1.useEffect)(() => { const hash = window.location.hash; const searchParams = new URLSearchParams(window.location.search); const loginError = searchParams.get('epic_login_error'); // Check for errors first if (loginError) { console.log(`Epic Login Failed: ${loginError}`); // Clear the error query parameters from the URL window.history.replaceState(null, '', window.location.pathname + window.location.hash); } // Handle successful login via hash if (hash.startsWith('#epic_jwt=')) { const epicJwt = hash.substring('#epic_jwt='.length); // Clear the hash from the URL window.history.replaceState(null, '', window.location.pathname + window.location.search); const signInWithEpic = async (token) => { try { storage?.setItem(localStorage_js_1.LocalStorageKey.WaasEpicIdToken, token); connect({ connector: socialAuthConnectors.find(c => c._wallet.id === 'epic-waas') }); } catch (err) { console.error('Sequence WaaS sign in failed:', err); } }; signInWithEpic(epicJwt); } }, []); return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [isPending && !isConnected && ((0, jsx_runtime_1.jsx)(design_system_1.Modal, { size: "sm", scroll: false, isDismissible: false, contentProps: { style: { maxWidth: '320px', padding: '20px' } }, children: (0, jsx_runtime_1.jsxs)("div", { className: "flex flex-col items-center text-black rounded-lg", children: [(0, jsx_runtime_1.jsx)("div", { className: "w-12 h-12 mb-4", "aria-label": "Epic Games", children: (0, jsx_runtime_1.jsx)(EpicLogo_js_1.EpicLogo, {}) }), (0, jsx_runtime_1.jsx)(design_system_1.ModalPrimitive.Title, { asChild: true, children: (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center gap-4 mt-4 mb-2 flex-row", children: [(0, jsx_runtime_1.jsx)("h2", { className: "text-white text-lg font-semibold text-center w-full", children: "Logging in with Epic Games\u2026" }), (0, jsx_runtime_1.jsx)(design_system_1.Spinner, {})] }) })] }) })), children] })); }; exports.EpicAuthProvider = EpicAuthProvider; //# sourceMappingURL=EpicAuthProvider.js.map