@toruslabs/customauth
Version:
CustomAuth login with torus to get user private key
61 lines (57 loc) • 1.89 kB
JavaScript
;
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var httpHelpers = require('@toruslabs/http-helpers');
var deepmerge = require('deepmerge');
var AbstractLoginHandler = require('./AbstractLoginHandler.js');
class GoogleHandler extends AbstractLoginHandler {
constructor(params) {
super(params);
_defineProperty(this, "RESPONSE_TYPE", "token id_token");
_defineProperty(this, "SCOPE", "profile email openid");
_defineProperty(this, "PROMPT", "select_account");
this.setFinalUrl();
}
setFinalUrl() {
const finalUrl = new URL("https://accounts.google.com/o/oauth2/v2/auth");
const clonedParams = JSON.parse(JSON.stringify(this.params.jwtParams || {}));
const finalJwtParams = deepmerge({
state: this.state,
response_type: this.RESPONSE_TYPE,
client_id: this.params.clientId,
prompt: this.PROMPT,
redirect_uri: this.params.redirect_uri,
scope: this.SCOPE,
nonce: this.nonce
}, 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 {
accessToken
} = params;
const userInfo = await httpHelpers.get("https://www.googleapis.com/userinfo/v2/me", {
headers: {
Authorization: `Bearer ${accessToken}`
}
});
const {
picture: profileImage = "",
email = "",
name = ""
} = userInfo;
return {
email,
name,
profileImage,
authConnectionId: this.params.authConnectionId,
authConnection: this.params.authConnection,
groupedAuthConnectionId: this.params.groupedAuthConnectionId,
userId: email.toLowerCase()
};
}
}
module.exports = GoogleHandler;