@toruslabs/customauth
Version:
CustomAuth login with torus to get user private key
70 lines (66 loc) • 2.53 kB
JavaScript
'use strict';
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var deepmerge = require('deepmerge');
var helpers = require('../utils/helpers.js');
var interfaces = require('../utils/interfaces.js');
var AbstractLoginHandler = require('./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 = helpers.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: helpers.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) || interfaces.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 = helpers.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
};
}
}
module.exports = Web3AuthPasswordlessHandler;