UNPKG

@turnkey/react-wallet-kit

Version:

The easiest and most powerful way to integrate Turnkey's Embedded Wallets into your React applications.

137 lines (133 loc) 3.51 kB
'use strict'; var sdkTypes = require('@turnkey/sdk-types'); var storage = require('./storage.js'); var config = require('./config.js'); /** * Unified OAuth completion handler. * Routes to the appropriate completion handler based on params: * Priority: onAddProvider > onOauthSuccess > callbacks.onOauthRedirect > completeOauth * * This is the core completion logic used by both popup and redirect flows. */ async function completeOAuthFlow(params) { const { provider, publicKey, oidcToken, sessionKey, callbacks, completeOauth, onOauthSuccess, onAddProvider } = params; if (onAddProvider) { await onAddProvider(oidcToken); } else if (onOauthSuccess) { onOauthSuccess({ publicKey, oidcToken, providerName: provider, ...(sessionKey && { sessionKey }) }); } else if (callbacks?.onOauthRedirect) { callbacks.onOauthRedirect({ idToken: oidcToken, publicKey, ...(sessionKey && { sessionKey }) }); } else { await completeOauth({ oidcToken, publicKey, providerName: provider, ...(sessionKey && { sessionKey }) }); } } /** * Completes OAuth flow in popup - handles both PKCE and non-PKCE providers */ async function completeOAuthPopup(params) { const { provider, publicKey, result, callbacks, completeOauth, onOauthSuccess, exchangeCodeForToken } = params; const config$1 = config.OAUTH_PROVIDER_CONFIGS[provider]; if (config$1.usesPKCE) { // PKCE flow, so we use completePKCEFlow if (!exchangeCodeForToken) { throw new sdkTypes.TurnkeyError("exchangeCodeForToken is required for PKCE providers", sdkTypes.TurnkeyErrorCodes.INTERNAL_ERROR); } await completePKCEFlow({ publicKey, providerName: provider, sessionKey: result.sessionKey, callbacks, completeOauth, onOauthSuccess, exchangeCodeForToken }); } else { // this is a non-PKCE flow, so we use our unified completion handler await completeOAuthFlow({ provider, publicKey, oidcToken: result.idToken, sessionKey: result.sessionKey, callbacks, completeOauth, onOauthSuccess }); } } /** * Unified PKCE flow handler for all PKCE-based OAuth providers. * Handles the complete PKCE flow: verifier retrieval, token exchange, and completion routing. * * This function abstracts the common logic shared between popup and redirect flows * for Facebook, Discord, and Twitter/X. * * @param params - The PKCE flow parameters * @returns A promise that resolves when the flow is complete */ async function completePKCEFlow({ publicKey, providerName, sessionKey, callbacks, completeOauth, onOauthSuccess, onAddProvider, exchangeCodeForToken }) { // Consume the verifier (retrieves and removes from storage) const verifier = storage.consumePKCEVerifier(providerName); // Exchange the code for an OIDC token using the provider-specific function const oidcToken = await exchangeCodeForToken(verifier); // Use unified completion handler await completeOAuthFlow({ provider: providerName, publicKey, oidcToken, sessionKey, callbacks, completeOauth, onOauthSuccess, onAddProvider }); } exports.completeOAuthFlow = completeOAuthFlow; exports.completeOAuthPopup = completeOAuthPopup; exports.completePKCEFlow = completePKCEFlow; //# sourceMappingURL=completion.js.map