Skip to content

Instantly share code, notes, and snippets.

@drovani
Last active Dec 24, 2019
Embed
What would you like to do?
Auth0 Progressive Profiling Proof-of-concept
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Auth0 Progressive Profiling</title>
<style>
body{
max-width: 900px;
margin-left: auto;
margin-right: auto;
}
label{
display: block;
}
</style>
</head>
<body>
<header>
<h1>Auth0 Progressing Profiling</h1>
<h2>Proof of Concept page</h2>
</header>
<main>
<form id="submitform" method="POST" action="https://{instance}.auth0.com/continue?state=">
<label>First name
<input name="given_name" required />
</label>
<label>Last name
<input name="family_name" required />
</label>
<button type="submit">Save Changes</button>
</form>
</main>
</body>
<script>
const urlparams = new URLSearchParams(window.location.search);
const stateval = urlparams.get('state');
const form = document.getElementById('submitform');
form.action = form.action + stateval;
</script>
</html>
function (user, context, callback) {
const RULE_NAME = 'Progressive Profiling';
console.log(`${RULE_NAME} started.`);
context.clientMetadata = context.clientMetadata || {};
const progprof_url = context.clientMetadata.progressive_profiling_url || configuration.progressive_profiling_url;
// skip if this doesn't need progressive profiling
if (!progprof_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) {
context.redirect = {
url: progprof_url
};
console.log(`${RULE_NAME}: Redirecting to ${context.redirect.url}`);
callback(null, user, context);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment