Last active
January 3, 2020 14:03
-
-
Save drovani/4b47e4563a3a4e5fc2466a3f3eae021c to your computer and use it in GitHub Desktop.
Auth0 Progressive Profiling rule
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) { | |
const RULE_NAME = 'Progressive Profiling'; | |
console.log(`${RULE_NAME} started.`); | |
// skip if this application doesn't need progressive profiling | |
if (!context.clientMetadata || !context.clientMetadata.progressive_profiling_url) { | |
return callback(null, user, context); | |
} | |
user.user_metadata = user.user_metadata || {}; | |
// if returning from the profile site | |
if (context.protocol === "redirect-callback") { | |
// build complete user profile | |
user.user_metadata = Object.assign(user.user_metadata, | |
_.pick( | |
context.request.body, | |
['given_name', 'family_name'])); | |
// update user profile in Auth0 | |
auth0.users.updateUserMetadata(user.user_id, user.user_metadata) | |
.then(() => { | |
console.log(`${RULE_NAME}: ${user.user_id}: Updated user profile with given_name "${user.user_metadata.given_name}" and family_name "${user.user_metadata.family_name}".`); | |
callback(null, user, context); | |
}) | |
.catch((err) => { | |
console.log(`${RULE_NAME} ERROR:`, err); | |
callback(err); | |
}); | |
} else { | |
// check if user already has profiled fields | |
if (!user.user_metadata || !user.user_metadata.given_name || !user.user_metadata.family_name) { | |
console.log(`${RULE_NAME}: Redirecting to ${context.clientMetadata.progressive_profiling_url}`); | |
context.redirect = { | |
url: context.clientMetadata.progressive_profiling_url | |
}; | |
} | |
callback(null, user, context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment