Hi, I’m trying to migrate the “Get email address from Twitter” rule to an action.
However, it fails with “Cannot find module ‘oauth-sign’”.
In the rule, there is no issue with require(‘oauth-sign’).
Can we have a list of the node libraries we can require in actions and why is it different from rules? Can we have an idea of what replaces the libraries we used in rules (in fact in templates provided in Auth0) such as oauth-sign?
Here’s my current action code:
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
if(event.connection.strategy!=='twitter')return;
// additional request below is specific to Twitter
let oauth=require('oauth-sign');
let request=require('request');
let uuid=require('uuid');
let _=require('underscore');
let url='https://api.twitter.com/1.1/account/verify_credentials.json';
let consumerKey='Agf4IJLE1bSwe1WmaTO9DPAH5';
let consumerSecretKey='aBaGNqd5ZH08kqWAG2TGQN0exGkoQIWKIAJXTarytAC2nypCn0';
let twitterIdentity=_.find(event.user.identities,{
connection:'twitter'
});
let oauthToken=twitterIdentity.access_token;
let oauthTokenSecret=twitterIdentity.access_token_secret;
let timestamp=Date.now()/1000;
let nonce=uuid.v4().replace(/-/g,'');
let params={
include_email:true,
oauth_consumer_key:consumerKey,
oauth_nonce:nonce,
oauth_signature_method:'HMAC-SHA1',
oauth_timestamp:timestamp,
oauth_token:oauthToken,
oauth_version:'1.0'
};
params.oauth_signature=oauth.hmacsign('GET',url,params,consumerSecretKey,oauthTokenSecret);
let auth=Object.keys(params).sort().map(function(k){
return k+'="'+oauth.rfc3986(params[k])+'"';
}).join(', ');
try{
let body=await new Promise((resolve,reject)=>request({
url:url+'?include_email=true',
headers:{
'Authorization':'OAuth '+auth
}
},(err,resp,body)=>err||resp.statusCode!==200?reject(new Error('Error retrieving email from twitter: '+body||err)):resolve(body)));
event.user.email=JSON.parse(body).email;
event.user.email_verified=true;
api.user.setUserMetadata('email',event.user.email);
}catch(error){
return api.access.deny(error.message);
}
};