UNPKG

nuxt-ual

Version:
105 lines (100 loc) 4.01 kB
'use strict'; const _app = require('#app'); const universalAuthenticatorLibrary = require('universal-authenticator-library'); const _NuxtUAL = class extends universalAuthenticatorLibrary.UAL { constructor(userCallbackHandler, chains, appName, authenticators) { super(chains, appName, authenticators); this.isAutologin = false; this.accountNameInputValue = ""; this.userCallbackHandler = userCallbackHandler; this.loginUser = this.loginUser.bind(this); } init() { const authenticators = this.getAuthenticators(); if (!!authenticators.autoLoginAuthenticator) { this.isAutologin = true; this.loginUser(authenticators.autoLoginAuthenticator); this.activeAuthenticator = authenticators.autoLoginAuthenticator; } else { this.attemptSessionLogin(authenticators.availableAuthenticators); } } attemptSessionLogin(authenticators) { const sessionExpiration = localStorage.getItem(_NuxtUAL.SESSION_EXPIRATION_KEY) || null; if (sessionExpiration) { if (new Date(sessionExpiration) <= new Date()) { localStorage.clear(); } else { const authenticatorName = localStorage.getItem(_NuxtUAL.SESSION_AUTHENTICATOR_KEY); const sessionAuthenticator = authenticators.find((authenticator) => authenticator.constructor.name === authenticatorName); const accountName = localStorage.getItem(_NuxtUAL.SESSION_ACCOUNT_NAME_KEY) || void 0; this.loginUser(sessionAuthenticator, accountName); } } } async loginUser(authenticator, accountName) { let users; this.activeAuthenticator = authenticator; const invalidateSeconds = this.activeAuthenticator.shouldInvalidateAfter(); const invalidateAt = new Date(); invalidateAt.setSeconds(invalidateAt.getSeconds() + invalidateSeconds); localStorage.setItem(_NuxtUAL.SESSION_EXPIRATION_KEY, invalidateAt.toString()); localStorage.setItem(_NuxtUAL.SESSION_AUTHENTICATOR_KEY, authenticator.constructor.name); try { await this.waitForAuthenticatorToLoad(authenticator); if (accountName) { users = await authenticator.login(accountName); localStorage.setItem(_NuxtUAL.SESSION_ACCOUNT_NAME_KEY, accountName); } else { users = await authenticator.login(); } this.userCallbackHandler(users); } catch (e) { console.error("Error", e); console.error("Error cause", e.cause ? e.cause : ""); this.clearStorageKeys(); throw e; } } async waitForAuthenticatorToLoad(authenticator) { return new Promise((resolve) => { if (!authenticator.isLoading()) { resolve(); return; } const authenticatorIsLoadingCheck = setInterval(() => { if (!authenticator.isLoading()) { clearInterval(authenticatorIsLoadingCheck); resolve(); } }, _NuxtUAL.AUTHENTICATOR_LOADING_INTERVAL); }); } async logoutUser() { if (!this.activeAuthenticator) { throw Error("No active authenticator defined, did you login before attempting to logout?"); } this.activeAuthenticator.logout(); this.clearStorageKeys(); } clearStorageKeys() { localStorage.removeItem(_NuxtUAL.SESSION_EXPIRATION_KEY); localStorage.removeItem(_NuxtUAL.SESSION_AUTHENTICATOR_KEY); localStorage.removeItem(_NuxtUAL.SESSION_ACCOUNT_NAME_KEY); } }; let NuxtUAL = _NuxtUAL; NuxtUAL.SESSION_EXPIRATION_KEY = "ual-session-expiration"; NuxtUAL.SESSION_AUTHENTICATOR_KEY = "ual-session-authenticator"; NuxtUAL.SESSION_ACCOUNT_NAME_KEY = "ual-session-account-name"; NuxtUAL.AUTHENTICATOR_LOADING_INTERVAL = 250; const plugin = _app.defineNuxtPlugin((nuxt) => { nuxt.provide("ual", (authenticators) => { const options = JSON.parse(`<%= JSON.stringify(options) %>`); const { appName, chainId, rpcEndpoints } = options; const nuxtUAL = new NuxtUAL(() => { }, [{ chainId, rpcEndpoints }], appName, authenticators); return nuxtUAL; }); }); module.exports = plugin;