Authenticate a user and request standard claims
In this example, we want to authenticate a user and get user details that will allow us to personalize our user interface. To do this, we need to get an that contains the user’s name, nickname, profile picture, and email information.-
Initiate the authentication flow by sending the user to the authorization URL:
Notice that in this example:
-
The
response_type
parameter includes one value:code
: because we are using the regular web app flow, our initial request is for an authorization code; when we request our tokens using this code, we will receive the ID Token we need for authentication.
-
The
scope
parameter includes three values; the requested OIDC scopes:openid
: to indicate that the application intends to use OIDC to verify the user’s identity.profile
: to getname
,nickname
, andpicture
.email
: to getemail
andemail_verified
.
-
The
- After the user consents (if necessary) and Auth0 redirects back to your app, request tokens.
-
Extract the ID token from the response and decode it. You should see the following claims:
Your app can now retrieve the user attributes and use them to personalize your UI.
Request custom API access
In this example, we request a custom scope for a calendar API that will authorize the calling application to read appointments for the user. To do this, we want to get an containing the proper scope to read appointments from the API. Note that requesting an access token is not dependent on requesting an ID token. Before using a custom API, you need to know what scopes are available for the API you are calling. If the custom API is under your control, you need to register both your application and API with Auth0 and define the scopes for your API using the Auth0 Dashboard. You can also use defined permissions to customize the consent prompt for your users.-
Initiate the authorization flow by sending the user to the authorization URL:
Notice that in this example:
-
The
response_type
parameter still includes one value:code
: because we are using the regular web app flow, our initial request is for an authorization code; when we request our tokens using this code, we will receive the Access Token that we can use to call our API.
-
the
scope
parameter includes one value; the requested API scope:read:appointments
: to allow us to read the user’s appointments from the API.
-
The
audience
parameter is new and includes one value:- The unique identifier of the API from which we want to read the user’s appointments.
-
The
- As in the previous example, after the user consents (if necessary) and Auth0 redirects back to your app, request tokens.
- Extract the access token from the response, and call the API using the access token as credentials.
Authenticate a user and request standard claims and custom API access
In this example, we combine our previous two examples to authenticate a user, request standard claims, and also request a custom scope for a calendar API that will allow the calling application to read appointments for the user. To do this, get two tokens:-
ID token that contains:
- User name
- Nickname
- Profile picture
- Email information
- Access token that contains the proper scope to read appointments from the API. Note that requesting an access token is not dependent on requesting an ID token.
-
Initiate the authentication flow by sending the user to the authorization URL:
Notice that in this example:
-
The
response_type
parameter still includes one value:code
: because we are using the regular web app flow, our initial request is for an authorization code; when we request our tokens using this code, we will receive both the ID token we need for authentication and the access token that we can use to call our API.
-
The
scope
parameter is used for both OIDC scopes and API scopes, so now includes four values:openid
: to indicate that the application intends to use OIDC to verify the user’s identity.profile
: to getname
,nickname
, andpicture
.email
: to getemail
andemail_verified
.read:appointments
: to allow us to read the user’s appointments from the API.
-
the
audience
parameter includes one value:- The unique identifier of the API from which we want to read the user’s appointments
-
The
- As in the previous examples, after the user consents (if necessary) and Auth0 redirects back to your app, request tokens.
- Extract the ID token from the response, decode it, and retrieve the user attributes and use them to personalize your UI.
- Extract the access token from the response, and call the API using the access token as credentials.
Add custom claims to a token
In this example, we add a user’s favorite color and preferred contact method to the ID Token. To do this, we create an Action to customize the ID token by adding these claims. Once added, we will also be able to obtain the custom claims when calling the/userinfo
endpoint (though the Action will run only during the authentication process).
Auth0 allows namespaced and non-namespaced claims, but certain restrictions apply (see General restrictions). To avoid name collisions, we recommend using namespaced claims. In case of collisions, the transaction won’t fail, but your custom claim won’t be added to your tokens.
- At some point, the user selected a
preferred_contact
method ofemail
and afavorite_color
ofred
, and we saved it as part of the user’suser_metadata
. - We’ve used the Management API or the Dashboard to set application-specific information for this user.
- The
sub
claim contains the value of theuser_id
property. - Neither the
favorite_color
noruser_metadata
properties are present because Connect (OIDC) does not define standard claims that representfavorite_color
oruser_metadata
.
- Navigate to Auth0 Dashboard > Actions > Library, and select Build Custom.
-
Enter a descriptive Name for your Action (for example,
Add user metadata to tokens
), select theLogin / Post Login
trigger because you’ll be adding the Action to the Login flow, then select Create. -
Locate the Actions Code Editor, copy the following JavaScript code into it, and select Save Draft to save your changes:
- From the Actions Code Editor sidebar, select Test (play icon), then select Run to test your code.
- When you’re ready for the Action to go live, select Deploy.
favorite_color
and preferred_contac
t custom claims in the ID Token:
api.idToken.setCustomClaims
method. To add these claims to an Access Token, use the api.accessToken.setCustomClaim
method.
To learn more about the event object for the trigger, read Actions Triggers: post-login - Event Object. To learn more about tokens, read Tokens.