Skip to content

Instantly share code, notes, and snippets.

@ramindu90
Created February 8, 2022 16:23
Show Gist options
  • Save ramindu90/b29f9d520f00677fc13ff5096c30638e to your computer and use it in GitHub Desktop.
Save ramindu90/b29f9d520f00677fc13ff5096c30638e to your computer and use it in GitHub Desktop.
Set roles to a user script in Auth0
function setRolesToUser(user, context, callback) {
// Roles should only be set to verified users.
if (!user.email || !user.email_verified) {
return callback(null, user, context);
}
user.app_metadata = user.app_metadata || {};
// You can add a Role based on what you want
// In this case I check domain
const addRolesToUser = function (user) {
return context.authorization.roles;
};
const roles = addRolesToUser(user);
user.app_metadata.roles = roles;
auth0.users
.updateAppMetadata(user.user_id, user.app_metadata)
.then(function () {
context.idToken['/roles'] = user.app_metadata.roles;
callback(null, user, context);
})
.catch(function (err) {
callback(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment