@toruslabs/customauth
Version:
CustomAuth login with torus to get user private key
59 lines (56 loc) • 1.85 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { get } from '@toruslabs/http-helpers';
import deepmerge from 'deepmerge';
import AbstractLoginHandler from './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 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()
};
}
}
export { GoogleHandler as default };