Skip to content

Instantly share code, notes, and snippets.

@drovani
Last active January 3, 2020 14:03
Show Gist options
  • Save drovani/4b47e4563a3a4e5fc2466a3f3eae021c to your computer and use it in GitHub Desktop.
Save drovani/4b47e4563a3a4e5fc2466a3f3eae021c to your computer and use it in GitHub Desktop.
Auth0 Progressive Profiling rule
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