# Rules and first login

**URL:** <https://community.auth0.com/t/rules-and-first-login/6201>\
**Category:** Get Help\
**Tags:** rules, lock, login\
**Created:** [April 12, 2017, 10:17pm UTC](https://community.auth0.com/t/rules-and-first-login/6201 "2017-04-12T22:17:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcmalpezzi](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@jcmalpezzi](https://community.auth0.com/u/jcmalpezzi)\
**Post date:** [April 12, 2017, 10:17pm UTC](https://community.auth0.com/t/rules-and-first-login/6201/1 "2017-04-12T22:17:55Z")

</div>

Hi everyone,

I just set my first rule, which is basically the standard _Set roles to a user_ rule.  
I only changed the domain to adapt it to my needs.

So here are the steps :

1. I create a new user manually, using the dashboard.
2. I log in with my application (code below).
3. In the auth0 dashboard, the user has the role property in app\_metadata.
4. But if i inspect the JWT in my app with my browser debugger, i do not have the role property in the payload.
5. I log out and log in again, the role is set and ready to use.

So am I doing something wrong ?  
I guess the rule is fired during the connection, maybe asynchronously making the first login payload not complete.

The front-end is Angular4, here is the Lock code :

```
  lock = new Auth0Lock(*myClientID*, *myDomain*,
  {
      auth: {
               redirect: true,
               redirectUrl: window.location.origin,
               responseType: "token",
               params: {scope: 'openid roles'}
            },
      language: 'fr',
      theme: {
                logo: '../../assets/img/logo_auth0.jpg',
                primaryColor: '#be0125'
             },
      allowSignUp: false,
      languageDictionary: {
                             title: "Veuillez vous identifier"
                          },
  });

```

Thank you for your help !  
jicey

---

<div class="post-metadata">

**Author:** ![prashantT](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prashantt/32/6002_2.png) [@prashantT](https://community.auth0.com/u/prashantT)\
**Post date:** [April 13, 2017, 9:42am UTC](https://community.auth0.com/t/rules-and-first-login/6201/2 "2017-04-13T09:42:22Z")

</div>

Metadata attributes are normalized to the root user profile before rules execution - this is why they show up on successive logins, but not during the first login. You can achieve this by doing the following inside your rule, after you have set the metadata:

```
user.roles = user.app_metadata.roles;

```

This will return the `roles` claim on first login as well.

---

<div class="post-metadata">

**Author:** ![jcmalpezzi](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@jcmalpezzi](https://community.auth0.com/u/jcmalpezzi)\
**Post date:** [April 13, 2017, 2:15pm UTC](https://community.auth0.com/t/rules-and-first-login/6201/3 "2017-04-13T14:15:50Z")

</div>

Works exactly as intended, thanks a lot !

Here is my final rule code :

```
function (user, context, callback) {
  user.app_metadata = user.app_metadata || {};
  // You can add a Role based on what you want
  // In this case I check domain
  var addRolesToUser = function(user, cb) {
    if (user.email.indexOf('@myDomainName') > -1) 
    {
      cb(null, 'admin']);
    } else 
    {
      cb(null, 'user']);
    }
  };

  addRolesToUser(user, function(err, roles) {
    if (err) 
    {
      callback(err);
    } else 
    {
      user.app_metadata.roles = roles;
      // ***New line below***
      user.roles = user.app_metadata.roles;
      
      auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
        .then(function(){
          callback(null, user, context);
        })
        .catch(function(err){
          callback(err);
        });
    }
  });
}

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 2, 2018, 5:53am UTC](https://community.auth0.com/t/rules-and-first-login/6201/4 "2018-03-02T05:53:14Z")

</div>


