Skip to content

Instantly share code, notes, and snippets.

@jacwright
Created May 8, 2017 15:41
Show Gist options
  • Save jacwright/469d3ad32aeb48271c15d83fde5c5180 to your computer and use it in GitHub Desktop.
Save jacwright/469d3ad32aeb48271c15d83fde5c5180 to your computer and use it in GitHub Desktop.
Auth0: Set unique ID for a user
function (user, context, callback) {
if (!user.app_metadata) {
user.app_metadata = {};
}
if (!user.app_metadata.userId || user.app_metadata.userId.length < 24) {
var idLength = 24;
var chars = (
'0123456789abcdefghijklmnopqrstuvwxyz'
).split('');
var id = '';
while (idLength--) {
id += chars[Math.random() * chars.length | 0];
}
user.app_metadata.userId = id;
auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
.then(function(){
callback(null, user, context);
})
.catch(function(err){
callback(err);
});
} else {
callback(null, user, context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment