I need to check if a user has existing roles.
My Current Rule adds a role “new” if the users login count < 2. I need to extend this to check if the user has been assigned any roles that where created with the management API.
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
if (context.stats.loginsCount < 2)
{
// You can add a Role based on what you want
// In this case I check domain
var addRolesToUser = function(user, cb) {
cb(null, 'new']);
};
addRolesToUser(user, function(err, roles) {
if (err) {
callback(err);
} else {
user.app_metadata.roles = roles;
auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
.then(function(){
context.idToken'https://syntrack.net/roles'] = user.app_metadata.roles;
callback(null, user, context);
})
.catch(function(err){
callback(err);
});
}
});
}
callback(null, user, context);
}
So would something like this work?
if (context.stats.loginsCount < 2)
{
if(user.app_metadata.roles === undefined)
{
//add the [new] role
}
}
callback(null, user, context);