Securing React Applications with Auth0
A guide for bootstrapping a new React app with Auth0

If you are looking for a comprehensive guide about how to create a new React application and set it up with Auth0 as the login provider then this is the article for you. I will be walking through a full demonstration from start to finish to show exactly how it’s done and give some pointers along the way.
Why use Auth0 for a login provider?
The startup I work has been using Auth0 from the start for their applications. We have it set up as the login provider for our web applications and our React Native mobile application.
Auth0 provides value by handling all the logins, sign up, and password management for your users. You get all of this out of the box without having to build UIs for login and password management.
This saves your engineering team a fair amount of time upfront because they don’t have to build and maintain all this themselves. Auth0 is also free to use up to a certain number of daily active users which is pretty generous. Note there are some premium features that you will have access to during the initial free preview window, but won’t be able to use unless you switch to a paid account after the initial preview period ends.
Getting setup with Auth0
Before we get started with building our demo application you will need to set yourself up with an Auth0 account.

After you have successfully created an account you will land on the following screen. We will want to create a new Single Page Application. Start by clicking on the Applications option in the menu.

This takes you to a list of your applications. We will want to click on “Create Application”.

The dialog will ask for a name for the new app and we will want to select the “Single Page Web Applications” option.

Once your application is created you can see the Domain, Client ID, and Client Secret. You will need to refer to this information later when we are building the application.

One more step before we jump into the app. There are three fields where you will need to enter the URL where you will be running your app. You can see in the screenshot below I’ve entered:
http://localhost:3000Create React App runs on port 3000 by default so that’s what I’ve entered here. If you want to run on another port you can switch to that.
Note that when you want to deploy your application elsewhere you will need to add that URL as well. You can add as many URLs as you want to as comma-separated values.

That wraps up everything we need as far as configuration. Now we can move on to setting up the demo application.
Demo Application
Now that we have everything configured we can start coding. First, we will use Create React App to bootstrap a new application and then install the Auth0 package via npm.
Run the following commands in a terminal to create the app, install the Auth0-React SDK, and finally run the app.
npx create-react-app auth0-react-demo
cd auth0-react-demo/
npm install @auth0/auth0-react
yarn startAuth0 uses React Context to manage the state of the user’s authentication status. The first code we add is adding the Auth0 Provider to the root of the application. You will want to replace domain and clientId with the values from your Auth0 application we created before.
src/index.js











