Add a Custom Claim To Auth0 JWT Id Token

InstructorWill Johnson

Share this video with your friends

Send Tweet

After a user successfully logs in, Auth0 sends an ID token to your React application. You can use the data from the ID token to personalize the user interface of your React application. ID tokens issued by Auth0 are JWTs and you can add custom claims to the JWTs using Actions.

Create a Login Action to add the user metadata as a customer claim.

exports.onExecutePostLogin = async (event, api) => {
  const namespace = `https://myapp.example.com`
  const { favorite_ninja_turtle } = event.user.user_metadata

  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/favorite_ninja_turtle`, favorite_ninja_turtle)
  }
};

Auth0 allows namespaced and non-namespaced claims.To avoid name collisions, we recommend using namespaced claims.

You can use any non-Auth0 HTTP or HTTPS URL as a namespace identifier.

Use a URL that you control as a namespace identifier; this allows you to avoid the risk that someone else is using the same namespace. The namespace URL does not have to point to an actual resource. It is only used as an identifier; it will not be called.

The JWT returned to the requesting application is built and signed at the end of the trigger running. The final, signed JWT is not accessible in an Action.

Check out the docs to learn more about creating custom claims