Last active
September 21, 2019 12:36
-
-
Save mathiasconradt/07a9c4f23a39e304060db88275ed62a1 to your computer and use it in GitHub Desktop.
Auth0 rule to enrich a user profile using the Pipl API
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
function (user, context, callback) { | |
// change to PIPL_SOCIAL_KEY or PIPL_CONTACT_KEY as needed | |
const PIPL_KEY = configuration.PIPL_BUSINESS_KEY; | |
// skip if Pipl metadata is already there | |
if (user.app_metadata && user.app_metadata.pipl) { | |
context.idToken['https://any-custom-namespace/pipl'] = user.app_metadata.pipl; | |
return callback(null, user, context); | |
} | |
var queryStrings = { | |
key: PIPL_KEY | |
}; | |
// add email to query as a minimum (if available) | |
if (user.email) { | |
queryStrings.email = user.email; | |
} | |
// in case of passwordless authentication via SMS | |
if (user.phone_number) { | |
queryStrings.phone = user.phone_number; | |
} | |
// add name to query if available in user profile | |
if (user.given_name && user.family_name) { | |
queryStrings.first_name = user.given_name; | |
queryStrings.last_name = user.family_name; | |
} | |
request.get('https://api.pipl.com/search/', { | |
qs: queryStrings, | |
json: true | |
}, (error, response, body) => { | |
if (error || (response && response.statusCode !== 200)) { | |
// swallow Pipl api errors and just continue login | |
return callback(null, user, context); | |
} | |
// if we reach here, it means Pipl returned info and we'll add it to the metadata | |
user.app_metadata = user.app_metadata || {}; | |
user.app_metadata.pipl = body; | |
auth0.users.updateAppMetadata(user.user_id, user.app_metadata); | |
context.idToken['https://any-custom-namespace/pipl'] = user.app_metadata.pipl; | |
return callback(null, user, context); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment