Auth0 Home Blog Docs

Roles for users how to read how to write

role

#1

Hi all,

I’m new here and in the authorisation in general.

I’m trying to assign a role to the user and then read it on a client. I use Vue and npm auth0, not a special Vue adaptation. I’ve made the simple authorisation works, thanks to the explicit lessons on this resource. I have successfully made a rule, at least it passed the tests.

Now I stuck with the question what to do next. I can not read roles in my app, although I see there is one by default in the user profile. The rule I’ve made pass the test but do not assign the right role. Maybe I send a wrong request how to check that?

I’ve read the forum most of the advice come to this article. https://auth0.com/docs/api-auth/tutorials/adoption/scope-custom-claims#custom-claims Honestly I do not understand how could I apply this.

I’ll be very appreciated for any help where to look and what to do.


#2

Please provide any logs and sample code for the community to review


#3

Honestly not too much of the code in here I probably do not understand in conception general.

On the page:
{{$auth.user.roles}} - returns empty string no worning no misstakes

In the component:
I use this lesson https://www.storyblok.com/tp/how-to-auth0-vuejs-authentication
The only place in the code which looks like the request is

    let webAuth = new auth0.WebAuth({
      domain: 'cornices.eu.auth0.com',
      clientID: 'AHS6j7tx5Jby4L24p0OoFq95j2XCDb0t',
      // make sure this line is contains the port: 8080
      redirectUri: 'http://localhost:8080/callback',
      // we will use the api/v2/ to access the user information as payload
      audience: 'https://cornices.eu.auth0.com/api/v2/', 
      responseType: 'token id_token',
      scope: 'openid profile roles',
      roles: 'pl'     // **so i've add this for the test**
    })

The role is:

function (user, context, callback) {
  user.app_metadata = user.app_metadata || {};
  var addRolesToUser = function(user, cb) {
    if (context.roles === 'pl') {
      cb(null, ['plasterer']);
    } else {
      cb(null, ['user']);
    }
  };
  addRolesToUser(user, function(err, roles) {
    if (err) {
      callback(err);
    } else {
      user.roles = user.app_metadata.roles; //**was added by the advice on this forum to avoide error when user doesn't exist**
      user.app_metadata.roles = roles;
      auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
        .then(function(){
          context.idToken['cornices.eu.auth0.com/roles'] = user.app_metadata.roles; //**was in the tamplate have know idea what it does**
          callback(null, user, context); //**was in the tamplate have know idea what it does**
        })
        .catch(function(err){
          callback(err);
        });
    }
    
  });
  callback(null, user, context);
}