UNPKG

@toruslabs/customauth

Version:

CustomAuth login with torus to get user private key

68 lines (65 loc) 2.53 kB
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import deepmerge from 'deepmerge'; import { validateAndConstructUrl, loginToConnectionMap, decodeToken } from '../utils/helpers.js'; import { EMAIL_FLOW } from '../utils/interfaces.js'; import AbstractLoginHandler from './AbstractLoginHandler.js'; class Web3AuthPasswordlessHandler extends AbstractLoginHandler { constructor(params) { super(params); _defineProperty(this, "SCOPE", "openid profile email"); _defineProperty(this, "RESPONSE_TYPE", "token id_token"); _defineProperty(this, "PROMPT", "login"); this.setFinalUrl(); } setFinalUrl() { const { domain } = this.params.jwtParams || {}; const finalUrl = validateAndConstructUrl(domain || "https://passwordless.web3auth.io/v7"); finalUrl.pathname += finalUrl.pathname.endsWith("/") ? "authorize" : "/authorize"; const clonedParams = JSON.parse(JSON.stringify(this.params.jwtParams || {})); delete clonedParams.domain; this.params.customState = _objectSpread(_objectSpread({}, this.params.customState || {}), {}, { client: this.params.web3AuthClientId }); const finalJwtParams = deepmerge({ state: this.state, client_id: this.params.clientId || this.params.web3AuthClientId, redirect_uri: this.params.redirect_uri, nonce: this.nonce, network: this.params.web3AuthNetwork, connection: loginToConnectionMap[this.params.authConnection], scope: this.SCOPE, response_type: this.RESPONSE_TYPE, prompt: this.PROMPT, flow_type: (clonedParams === null || clonedParams === void 0 ? void 0 : clonedParams.flow_type) || EMAIL_FLOW.code }, clonedParams); Object.keys(finalJwtParams).forEach(key => { const localKey = key; if (finalJwtParams[localKey]) finalUrl.searchParams.append(localKey, finalJwtParams[localKey]); }); this.finalURL = finalUrl; } async getUserInfo(params) { const { idToken } = params; const decodedToken = decodeToken(idToken).payload; const { name, email, picture } = decodedToken; return { profileImage: picture, name, email, userId: name.toLowerCase(), authConnectionId: this.params.authConnectionId, authConnection: this.params.authConnection, groupedAuthConnectionId: this.params.groupedAuthConnectionId }; } } export { Web3AuthPasswordlessHandler as default };