@0xsequence/connect
Version:
Connect package for Sequence Web SDK
47 lines • 3.09 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Modal, ModalPrimitive, Spinner } from '@0xsequence/design-system';
import { useEffect } from 'react';
import { useAccount, useConnect } from 'wagmi';
import { EpicLogo } from '../../connectors/epic/EpicLogo.js';
import { LocalStorageKey } from '../../constants/localStorage.js';
import { useStorage } from '../../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.
export const EpicAuthProvider = ({ children }) => {
const { connectors, connect, isPending } = useConnect();
const { isConnected } = useAccount();
const storage = 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
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(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 (_jsxs(_Fragment, { children: [isPending && !isConnected && (_jsx(Modal, { size: "sm", scroll: false, isDismissible: false, contentProps: { style: { maxWidth: '320px', padding: '20px' } }, children: _jsxs("div", { className: "flex flex-col items-center text-black rounded-lg", children: [_jsx("div", { className: "w-12 h-12 mb-4", "aria-label": "Epic Games", children: _jsx(EpicLogo, {}) }), _jsx(ModalPrimitive.Title, { asChild: true, children: _jsxs("div", { className: "flex items-center gap-4 mt-4 mb-2 flex-row", children: [_jsx("h2", { className: "text-white text-lg font-semibold text-center w-full", children: "Logging in with Epic Games\u2026" }), _jsx(Spinner, {})] }) })] }) })), children] }));
};
//# sourceMappingURL=EpicAuthProvider.js.map