JavaScript Rules

Sample Project

Download this sample project configured with your Auth0 API Keys.

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 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

You can use any of the pre-built rules or create your own 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 the Rule

To see the results of a created rule, log in and fetch the user’s profile information, as explained in the User Profile step.

Then you can display the user profile's new country attribute added by the rule:

<!-- index.html -->

<div id="logged" class="row" style="display: none;">
  <h4>You are logged in</h4>
  <div class="row">
    <div class="col-md-6">
      <img alt="" id="avatar">
      <p><strong>Country (added by rule): </strong> <span id="country"></span></p>
  <button type="button" class="btn btn-default" id="btn-logout">Logout</button>
// app.js

var showUserProfile = function(profile) {
  // ...
  document.getElementById('country').textContent =;

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