UNPKG

@web3auth/modal

Version:

Multi chain wallet aggregator for web3Auth

120 lines (117 loc) 4.42 kB
import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { post } from '@toruslabs/http-helpers'; import { BUILD_ENV, AUTH_CONNECTION, storageAvailable } from '@web3auth/auth'; import { WalletInitializationError } from '@web3auth/no-modal'; import { version } from '../../config.js'; import { PASSWORDLESS_BUILD_ENV_MAP } from '../config.js'; import { getErrorMessages } from '../utils.js'; class PasswordlessHandler { constructor(params) { _defineProperty(this, "authBaseApiUrl", void 0); _defineProperty(this, "passwordlessParams", void 0); _defineProperty(this, "trackingIdentifier", void 0); _defineProperty(this, "version", `web3auth-${version.split(".")[0]}`); if (!params.authConnection) throw WalletInitializationError.invalidParams("authConnection is required"); if (!params.web3authClientId) throw WalletInitializationError.invalidParams("web3authClientId is required"); if (!params.loginHint) throw WalletInitializationError.invalidParams("loginHint is required"); if (!params.network) throw WalletInitializationError.invalidParams("network is required"); if (!params.authBuildEnv) params.authBuildEnv = BUILD_ENV.PRODUCTION; this.authBaseApiUrl = `${PASSWORDLESS_BUILD_ENV_MAP[params.authBuildEnv]}/api/v3/auth`; this.passwordlessParams = params; } get name() { if (this.passwordlessParams.authConnection === AUTH_CONNECTION.EMAIL_PASSWORDLESS) return "Email"; if (this.passwordlessParams.authConnection === AUTH_CONNECTION.SMS_PASSWORDLESS) return "Mobile"; throw WalletInitializationError.invalidParams("Invalid authConnection"); } get connection() { if (this.passwordlessParams.authConnection === AUTH_CONNECTION.EMAIL_PASSWORDLESS) return "email"; if (this.passwordlessParams.authConnection === AUTH_CONNECTION.SMS_PASSWORDLESS) return "sms"; throw WalletInitializationError.invalidParams("Invalid authConnection"); } get trackingId() { return this.trackingIdentifier; } get sessionStorageAvailable() { return storageAvailable("sessionStorage"); } get whiteLabelParams() { var _ref; const { uiConfig } = this.passwordlessParams; if (!uiConfig) return {}; const { appName, appUrl, defaultLanguage, mode, logoLight, logoDark, theme } = uiConfig; const finalLogo = (_ref = mode === "dark" ? logoDark : logoLight) !== null && _ref !== void 0 ? _ref : ""; return { mode: mode !== null && mode !== void 0 ? mode : "light", name: appName !== null && appName !== void 0 ? appName : "", url: appUrl !== null && appUrl !== void 0 ? appUrl : "", language: defaultLanguage !== null && defaultLanguage !== void 0 ? defaultLanguage : "en", logo: finalLogo.includes(".svg") ? "" : finalLogo, theme: theme !== null && theme !== void 0 ? theme : {} }; } set trackingId(value) { this.trackingIdentifier = value; } async start(params) { try { const result = await post(`${this.authBaseApiUrl}/passwordless/start`, params); if (result && result.success) { var _result$data; this.trackingId = (_result$data = result.data) === null || _result$data === void 0 ? void 0 : _result$data.trackingId; if (this.sessionStorageAvailable) window.sessionStorage.setItem("trackingId", this.trackingId); } return result; } catch (e) { return this.handleError(e); } } async verify(params) { try { const result = await post(`${this.authBaseApiUrl}/passwordless/verify`, params); if (result.success) { if (this.sessionStorageAvailable) window.sessionStorage.removeItem("trackingId"); return { success: true, data: { id_token: result.id_token } }; } return { success: false, error: result.message }; } catch (e) { return this.handleError(e); } } async handleError(e) { let error; if (e.status === 429) { error = "passwordless.error-too-many-requests"; } else { try { const err = await e.json(); error = err.error_code ? getErrorMessages(err.error_code) : err.message; } catch { error = "passwordless.something-wrong-error"; } } return { success: false, error: error }; } } export { PasswordlessHandler };