Giter VIP home page Giter VIP logo

auth0-react-sample-classes's Introduction

Easy User Authentication for React Apps (Sample with Classes)

This repository hosts a React project that defines a Single-Page Application (SPA). You'll secure access to some of its routes using Auth0 User Authentication.

Get Started

Install the client project dependencies:

npm install

Run the client project:

npm start

The application runs by on port 4040 to mitigate conflicting with other client applications you may be running.

Visit http://localhost:4040/ to access the application.

Get Started with Auth0

Sign up and create an Auth0 Application

If you haven't already, sign up for a free Auth0 account.

Once you sign in, Auth0 takes you to the Dashboard. In the left sidebar menu, click on "Applications".

Then, click the "Create Application" button. A modal opens up with a form to provide a name for the application and choose its type.

  • Name: Auth0 React Sample

  • Application Type: Single Page Web Applications

Click the "Create" button to complete the process. Your Auth0 application page loads up.

Click on the "Settings" tab of your Auth0 Application page and fill in the following values:

๐Ÿ›  Allowed Callback URL

http://localhost:4040

The above value is the URL that Auth0 can use to redirect your users after they successfully log in.

๐Ÿ›  Allowed Logout URL

http://localhost:4040

The above value is the URL that Auth0 can use to redirect your users after they log out.

๐Ÿ›  Allowed Web Origins

http://localhost:4040

Using the Auth0 React SDK, your React application will make requests under the hood to an Auth0 URL to handle authentication requests. As such, you need to add your React application origin URL to avoid Cross-Origin Resource Sharing (CORS) issues.

Scroll down and click the "Save Changes" button.

Do not close this page yet. You'll need some of its information in the next section.

Add the Auth0 configuration variables to React

From the Auth0 Application Settings page, you need the Auth0 Domain and Client ID values to allow your React application to use the communication bridge you created.

Open the React starter project, auth0-react-sample-classes, and create a .env file under the project directory:

touch .env

Populate .env as follows:

REACT_APP_AUTH0_DOMAIN=
REACT_APP_AUTH0_CLIENT_ID=
REACT_APP_AUTH0_AUDIENCE=https://express.sample
REACT_APP_SERVER_URL=http://localhost:6060

The value of REACT_APP_AUTH0_DOMAIN is the "Domain" value from the "Settings".

The value of REACT_APP_AUTH0_CLIENT_ID is the "Client ID" value from the "Settings".

Auth0 Application Settings from the Auth0 Dashboard

These variables let your React application identify itself as an authorized party to interact with the Auth0 authentication server.

Get the Express API demo

You need this Express server to test making secure API calls from the React application sample.

Open a new terminal window and clone the auth0-express-js-sample repo somewhere in your system. Ensure that you clone it outside of your React project directory.

git clone [email protected]:auth0-blog/auth0-express-js-sample.git

Once you clone this repo, make the auth0-express-js-sample directory your current directory:

cd auth0-express-js-sample

Install the Node.js project dependencies:

npm install

Connect the Express API with Auth0

This process is similar to how you connected React with Auth0.

Head to the APIs section in the Auth0 Dashboard, and click the "Create API" button.

Then, in the form that Auth0 shows:

  • Add a Name to your API: Auth0 Express Sample

  • Set its Identifier value: https://express.sample

  • Leave the signing algorithm as RS256 as it's the best option from a security standpoint.

The Identifier is the same as the REACT_APP_AUTH0_AUDIENCE value in the .env file from your React application.

Auth0 Dashboard new API form

With these values in place, hit the "Create" button.

Now, click on the "Quick Start" tab of your Auth0 API page. This page presents instructions on how to set up different APIs. From the code box, choose "Node.js". Keep this page open as you'll be using the values next.

Auth0 API Node.js Quickstart

Create a .env file for the API Server under the auth0-express-sample directory:

touch .env

Populate this auth0-express-sample/.env file as follows:

SERVER_PORT=6060
CLIENT_ORIGIN_URL=http://localhost:4040
AUTH0_AUDIENCE=
AUTH0_ISSUER_URL=

Head back to the "Node.js" code snippet from the Auth0 API "Quick Start" page. Locate the definition of jwtCheck:

var jwtCheck = jwt({
  secret: jwks.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 5,
    jwksUri: "https://<TENANT-NAME>.auth0.com/.well-known/jwks.json",
  }),
  audience: "https://express.sample", // ๐Ÿ‘ˆ AUTH0_AUDIENCE value
  issuer: "https://<TENANT-NAME>.auth0.com/", // ๐Ÿ‘ˆ AUTH0_ISSUER_URL value
  algorithms: ["RS256"],
});

Look at the object that the jwt function takes as an argument and use the following properties to complete the values of your .env file:

The audience property is the value of AUTH0_AUDIENCE.

The issuer property is the value of AUTH0_ISSUER_URL.

Run the Express server

With the .env configuration values set, run the API server by issuing the following command:

npm start

auth0-react-sample-classes's People

Contributors

danoncall avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.