Created
February 8, 2022 16:23
-
-
Save ramindu90/b29f9d520f00677fc13ff5096c30638e to your computer and use it in GitHub Desktop.
Set roles to a user script in Auth0
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 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