React: Calling an API

View on Github

React: Calling an API

Gravatar for
By Andres Aguiar

This tutorial demonstrates how to make API calls for protected resources on your server. We recommend you to Log in to follow this quickstart with examples configured for your account.

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: React 15.6

Most single-page apps use resources from data APIs. You may want to restrict access to those resources, so that only authenticated users with sufficient privileges can access them. Auth0 lets you manage access to these resources using API Authorization.

This tutorial shows you how to access protected resources in your API.

This tutorial does not show you how to add protection to your API. Read the Backend/API quickstart documentation for instructions on how to protect your API.

Create an API

In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API. You will use the identifier later when you're configuring your Javascript Auth0 application instance. For Signing Algorithm, select RS256.

Create API

Add a Scope

By default, the Access Token does not contain any authorization information. To limit access to your resources based on authorization, you must use scopes. Read more about scopes in the scopes documentation.

In the Auth0 dashboard, in the APIs section, click Scopes. Add any scopes you need to limit access to your API resources.

You can give any names to your scopes. A common pattern is <action>:<resource>. The example below uses the name read:messages for a scope.

create scope

Configure your Application

In your auth0.WebAuth instance, enter your API identifier as the value for audience. Add your scopes to the scope key.

Checkpoint: Try to log in to your application again. Look at how the Access Token differs from before. It is no longer an opaque token. It is now a JSON Web Token with a payload that contains your API identifier as the value for audience, and the scopes you created. Read more about this token in the JSON Web Tokens documentation.

By default, any user can ask for any scope you defined. You can implement access policies to limit this behavior using Rules.

Send Authenticated HTTP Requests

To give the authenticated user access to secured resources in your API, include the user's Access Token in the requests you send to your API. There are two common ways to do this.

  • Store the Access Token in a cookie. The Access Token is then included in all requests.
  • Send access_token in the Authorization header using the Bearer scheme.

The examples below use the Bearer scheme.

The example below shows how to add the user's Access Token as the Authorization header to HTTP calls using axios. You can use any HTTP client you want.

Protect Your API Resources

To restrict access to the resources served by your API, check the incoming requests for valid authorization information. The authorization information is in the Access Token created for the user. To see if the token is valid, check it against the JSON Web Key Set (JWKS) for your Auth0 account. To learn more about validating Access Tokens, read the Verify Access Tokens tutorial.

In each language and framework, you verify the Access Token differently. Typically, you use a middleware function to verify the token. If the token is valid, the request proceeds and the user gets access to resources in your API. If the token is invalid, the request is rejected with a 401 Unauthorized error.

The sample project shows how to implement this functionality using Node.js with the Express framework. To learn how to implement API protection for your server-side technology, see the Backend/API quickstart documentation.

Use Auth0 for FREE