Javascript Single Page App Rules

Seed Project

Download a sample project. You will need to login for the sample to be preconfigured.

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

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 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
Try Auth0 for FREECreate free Account