This TypeScript code sample demonstrates how to implement authentication in a React Single-Page Application (SPA) using the Auth0 React SDK. This React code sample implements the following security tasks:
- Add user login and logout.
- Retrieve user profile information.
- Protect application routes.
- Make secure calls to an API.
Code Sample Specs
This code sample uses the following main tooling versions:
- React Router
- Auth0 React SDK
The React project dependency installations were tested with npm
v7.24.0. Running the React application was tested using Node.js
Quick Auth0 Set Up
First and foremost, if you haven't already, sign up for an Auth0 account to connect your application with the Auth0 Identity Platform.
Next, you'll connect your Single-Page Application (SPA) with Auth0. You'll need to create an application registration in the Auth0 Dashboard and get two configuration values: the Auth0 Domain and the Auth0 Client ID. You'll also need to define an Auth0 Audience value within your project to practice making secure calls to an external API.
Get the Auth0 domain and client ID
Open the Applications section of the Auth0 Dashboard.
Click on the Create Application button and fill out the form with the following values:
- Click on the Create button.
Visit the "Register Applications" document for more details.
An Auth0 Application page loads up.
As such, click on the "Settings" tab of your Auth0 Application page, locate the "Application URIs" section, and fill in the following values:
Scroll down and click the "Save Changes" button.
Next, locate the "Basic Information" section.
When you enter a value in the input fields present on this page, any code snippet that uses such value updates to reflect it. Using the input fields makes it easy to copy and paste code as you follow along. For security, these values are stored in memory and only used locally. They are gone as soon as you refresh the page! As an extra precaution, you should use values from an Auth0 test application instead of a production one.
As such, enter the "Domain" and "Client ID" values in the following fields to set up your single-page application in the next section:
Set Up the React TypeScript Project
Start by cloning the project into your local machine:
git clone https://github.com/auth0-developer-hub/spa_react_typescript_hello-world.git --branch basic-authentication
You are checking out the
basic-authentication branch, which holds all the React code related to implementing user login with Auth0.
Make the project directory your current working directory:
Next, install the React project dependencies:
Once you have access to the React project, create a
.env file under the project directory and populate it as follows:
Run the React application by issuing the following command:
You can now visit
http://localhost:4040/ to access the application.
There's something missing in this React code sample. There's no login or sign-up forms!
When you click on the "Log In" button, React takes you to the Auth0 Universal Login page. Your users can log in to your application through a page hosted by Auth0, which provides them with a secure, standards-based login experience that you can customize with your own branding and various authentication methods, such as logging in with username and password or with a social provider like Facebook or Google.
Then, visit the "External API" page,
http://localhost:4040/external-api, to practice requesting protected resources from an external API server using access tokens. Before you can do that, you need to set up and configure an API with Auth0. It'll be quick!
Make Requests to an External API
Let's simulate an essential feature of client applications: requesting data from an API server.
You can pair this client application with an API server that matches the technology stack that you use at work. This "Hello World" client application can communicate with any of our "Hello World" API server samples.
You can simulate a secure full-stack application system in no time. Each API server sample gives you clear instructions to get it up and running quickly.
Once set up, you can test the client-server connection in the
http://localhost:4040/external-api page of your client application.
Pick an API code sample in your preferred backend framework and language: