How do rules work?

I started exploring auth0 today and I don’t get how rules are meant to be implemented.

What do I do with this?

function (user, context, callback) {
  context.idToken["http://mynamespace/hello"] = "world";
  console.log('===> set "hello" for ' + user.name);
  callback(null, user, context);
}

should I call it somewhere, or export it and import it to somewhere else, what do I do?

I also can’t find anywhere what types I should give to the arguments when using Typescript.

Hi,

Your rule is attaching a custom claim to an ID Token.
The ID Token will be returned to your application after the authentication completes.
your application could consume that claim for some purpose.

Example of ID token with the claim from the rule above:

{
==>  "http://mynamespace/hello": "world",
  "nickname": "marcus.baker@auth0.com",
  "name": "marcus.baker@auth0.com",
  "updated_at": "2021-08-31T01:37:42.520Z",
  "iss": "https://mytenant.auth0.com/",
  "sub": "auth0|dbuser",
  "aud": "4kHx.................eQEm",
  "iat": 16........,
  "exp": 1630......,
  "nonce": "UkJocDVUVGQ4R2VOQkxk......Y0RlT0s2MUZYS0FTflZ0QkU0Yw=="
}

We have some use cases for claims documented here: Sample Use Cases: Scopes and Claims

In addition to Rules, it is worth checking out Auth0 Actions, our newest extensibility tool. All the power of Rules with a much-improved editing and deployment experience.

Hope this helps!

2 Likes

Thanks for helping on this one Marcus!

Yes but how do I make it work? Do I just create the rule in the dashboard?

Hey!

Yes, you would put that code in the editor window in a new Rule.

From the dashboard - click:

Auth Pipeline
Rules
Create
Empty Rule

The new rule editor should load in the dashboard.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.