iOS-Swift Rules

Seed Project

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

Rules are one of the cool features of Auth0. The reason behind that coolness is their flexibility, which gives you the ability to extend what Auth0 has to offer. They are just JavaScript functions which work like middlewares. To see a detailed description, please refer to the full documentation.

Create a Rule

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

Let's use the template called "Add country to the user profile", under the Enrich Profile section:

Add country template

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

Country rule sample

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

Test the Rule

To see the created rule results, just implement a login and fetch the user profile information (you can check out how to do it in the login tutorial).

You can access the country added by the rule within the extraInfo dictionary from the A0UserProfile object, like so:

import Lock
let profile: A0UserProfile = ... // the user profile you get upon login
guard let country = profile.extraInfo["country"] as? String else {
    // test failed
print("user country is: \(country)")
Previous Tutorial
6. Linking Accounts
Next Tutorial
8. Authorization
Try Auth0 for FREECreate free Account