Ruby On Rails Rules

Rules are one of the most powerful features of Auth0. As JavaScript functions that execute each time a user authenticates, rules serve as middleware to extend the flexibility of your authentication flow. For a detailed description of rules, see the full documentation.

Create a Rule

To create a rule, go to the new rule page. You can create a new rule from scratch or use an existing template. These templates are written by Auth0 team to assist you complete common tasks.

Select the template called "Add country to the user profile"

Empty rule

This rule simply gets the country_name from the context and adds it as a new country attribute to the user profile.

Add country rule

This is just a starting template, and you can edit it to meet your business needs. Once you are done, save the rule and that's it. Whenever a user logs in, the rule will be executed, and the country will be added to the user's profile.

Create a Rule

To create a rule, go to the New Rule page. You can create a rule from scratch by selecting an empty rule, or you may use one of the existing templates. These templates are written by Auth0 to cover common scenarios and use cases.

For this example, select the Add country to the user profile rule.

Empty rule

This rule extracts the country_name from the context and adds it to the user profile as a new country attribute.

Add country rule

This is just the beginning, as you can edit the rule to meet your business needs. Once complete, click Save. The rule will execute any time a user logs in, and the country will be added to the user's profile.

Test Rule Result

Country rule sample

NOTE: No additional code is required, as the new property will be included in the full user profile.

After the rule executes, the output that the application will receive for the full user profile is the following user object:

{
  "email": "test@test.com",
  "nickname": "test",
  "user_id": "auth0|575acb630e8da29012778689",
  //...
  "country": "Argentina"  // NEW PROPERTY ADDED BY THE RULE
}

This is just one of the tons of cool things we can do with rules. Go ahead and create any rule that fits your needs.

Previous Tutorial
6. Linking Accounts
Next Tutorial
8. Authorization
Use Auth0 for FREECreate free Account