Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Last active October 31, 2023 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshcanhelp/26e1fc3581d2ced737c4f50a694ea10f to your computer and use it in GitHub Desktop.
Save joshcanhelp/26e1fc3581d2ced737c4f50a694ea10f to your computer and use it in GitHub Desktop.
/* globals request */
module.exports = function fetchUserProfile(accessToken, context, callback) {
request.get({
// TODO: Update this URL with the userinfo endpoint.
url: 'https://example.com/userinfo',
// TODO: Maybe update this based on access token use.
headers: {
'Authorization': 'Bearer ' + accessToken,
}
},
(err, resp, body) => {
if (err) {
return callback(err);
}
if (resp.statusCode !== 200) {
return callback(new Error(`[Response code: ${resp.statusCode}] ${body}`));
}
let bodyParsed;
try {
bodyParsed = JSON.parse(body);
} catch (jsonError) {
return callback(new Error(body));
}
// Use the Real-time Webtask Logs extension
console.log(bodyParsed);
// TODO: Update this to map the incoming user identity to the Auth0 profile:
// https://auth0.com/docs/users/user-profile-structure
bodyParsed.user_id = bodyParsed.sub;
// You can also add custom data to user metadata
bodyParsed.app_metadata = {
_namespace: {
customerId: bodyParsed.account.customer_id,
valueTier: bodyParsed.account.customer_ltv,
}
};
return callback(null, bodyParsed);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment