-
-
Save joshcanhelp/26e1fc3581d2ced737c4f50a694ea10f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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