Skip to content

Instantly share code, notes, and snippets.

@ankkal
Last active July 23, 2018 04:58
Show Gist options
  • Save ankkal/2fb4d228e6a78eb0e0e660e743995841 to your computer and use it in GitHub Desktop.
Save ankkal/2fb4d228e6a78eb0e0e660e743995841 to your computer and use it in GitHub Desktop.
//Add this code inside your Intent Handler
if (handlerInput.requestEnvelope.context.System.user.accessToken !== undefined) {
accessToken = handlerInput.requestEnvelope.context.System.user.accessToken;
try {
var tokenOptions = buildHttpGetOptions(accessToken);
var response = await httpGet(tokenOptions);
console.log({ response });
if (response.phone_number.length > 0) {
phoneNumber = response.phone_number;
const sessionAttributes = attributesManager.getSessionAttributes();
sessionAttributes.phoneNumber = phoneNumber;
}
}
catch (error) {
console.log(`Error message: ${error.message}`);
}
}
//Helper Function for calling the Cognito /oauth2/userInfo to get user info using the accesstoken
var https = require('https');
// https is a default part of Node.JS. Read the developer doc: https://nodejs.org/api/https.html
function buildHttpGetOptions(accessToken) {
return {
//Replace the host with your cognito user pool domain
host: 'accountlinkingdemo.auth.ap-south-1.amazoncognito.com',
port: 443,
path: '/oauth2/userInfo',
method: 'GET',
headers: {
'authorization': 'Bearer ' + accessToken
}
};
}
function httpGet(options) {
return new Promise(((resolve, reject) => {
var request = https.request(options, (response) => {
response.setEncoding('utf8');
let returnData = '';
if (response.statusCode < 200 || response.statusCode >= 300) {
return reject(new Error(`${response.statusCode}: ${response.req.getHeader('host')} ${response.req.path}`));
}
response.on('data', (chunk) => {
returnData += chunk;
});
response.on('end', () => {
console.log({ returnData });
resolve(JSON.parse(returnData));
});
response.on('error', (error) => {
reject(error);
});
});
request.end();
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment