@toruslabs/customauth
Version:
CustomAuth login with torus to get user private key
61 lines (57 loc) • 1.86 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 TwitchHandler extends AbstractLoginHandler {
constructor(params) {
super(params);
_defineProperty(this, "RESPONSE_TYPE", "token");
_defineProperty(this, "SCOPE", "user:read:email");
this.setFinalUrl();
}
setFinalUrl() {
const finalUrl = new URL("https://id.twitch.tv/oauth2/authorize");
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,
redirect_uri: this.params.redirect_uri,
scope: this.SCOPE,
force_verify: "true"
}, 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://api.twitch.tv/helix/users", {
headers: {
Authorization: `Bearer ${accessToken}`,
"Client-ID": this.params.clientId
}
});
const [{
profile_image_url: profileImage = "",
display_name: name = "",
email = "",
id: userId
}] = userInfo.data || [];
return {
profileImage,
name,
email,
userId: userId,
authConnectionId: this.params.authConnectionId,
authConnection: this.params.authConnection,
groupedAuthConnectionId: this.params.groupedAuthConnectionId
};
}
}
module.exports = TwitchHandler;