Note: This document is only for Classic Engine. If you’re using Okta Identity Engine, see Sign in to SPA with Auth JS. See Identify your Okta solution (opens new window) to determine your Okta version.
This guide walks you through integrating authentication into a React app with Okta by performing these steps:
Note: This guide is for @okta/okta-auth-js
v5.1.1 and @okta/okta-react
v6.0.0.
Prerequisites
If you don’t already have an Okta Developer Edition org, you can create one at https://developer.okta.com/signup/ (opens new window).
Add an OpenID Connect client in Okta
- Sign in to the Okta developer dashboard, and select Create New App
- Choose Single Page App (SPA) as the platform, then populate your new OpenID Connect app with appropriate values for your app. For example:
Setting | Value |
App name | OpenID Connect App (must be unique) |
Login redirect URIs | http://localhost:3000/login/callback |
Logout redirect URIs | http://localhost:3000/ |
Allowed grant types | Authorization Code |
Note: It's important to choose the appropriate app type for apps that are public clients. Failing to do so may result in Okta API endpoints attempting to verify an app's client secret. Public clients aren’t designed to a client secret, hence breaking the sign-in or sign-out flow.
Note: CORS is automatically enabled for the granted login redirect URIs.
Create a React app
To create a React app, you can use Create React App (opens new window):
This creates a project in a folder named okta-app
and installs all required dependencies.
Install dependencies
A simple way to add authentication to a React app is to use auth.js. You can install it using npm
:
You also need @okta/okta-react
and react-router-dom
to manage your routes. You can use @okta/okta-react
to support other router libraries, but react-router-dom
has pre-existing support.
Auth.js provides more options that the Sign-in Widget: user lifecycle operations, MFA, and more. In this example, you create a simple username and password form without multifactor authentication.
Create src/SignInForm.jsx
using a function-based component:
Create src/SignInForm.jsx
using a class-based component:
Create routes
Some routes require authentication before rendering. Defining those routes is easy using SecureRoute
from @okta/okta-react
. Let's look at what routes are needed for this example:
/
: A default page to handle basic control of the app. /protected
: A route protected by SecureRoute
. /login
: Redirect to the org sign-in page. /login/callback
: A route to parse tokens after a redirect.
/
{#slash-root}
First, create src/Home.jsx
to provide links to navigate our app using a function-based component:
Create src/Home.jsx
using a class-based component:
/protected
{#slash-protected}
Create src/Protected.jsx
. This route will only be visible to users with a valid accessToken
:
/login
{#slash-login}
This route redirects if the user is already signed in. If the user is coming from a protected page, they’re redirected back to the page upon successful sign-in.
Create src/SignIn.jsx
using a function-based component:
src/SignIn.jsx
using a class-based component:
/login/callback
{#slash-callback}
The component for this route (LoginCallback) comes with @okta/okta-react
. It handles token parsing, token storage, and redirecting to a protected page if one triggered the sign in.
Connect the routes
Update src/App.js
to include your project components and routes. Security
is the component that controls the authentication flows, so it requires your OpenID Connect configuration. By default, @okta/okta-react
redirects to Okta's sign-in page when the user isn't authenticated.
In this example, onAuthRequired
is overridden to redirect to the custom sign-in route instead, which requires a component that is a descendent of Router
to have access to react-router
's history
. Other router libraries have their own methods of managing browser history:
Update src/App.js
to use function-based components:
And, create its companion at src/AppWithRouterAccess.jsx
. Make sure to replace the {...}
placeholders with your Okta values.
Update src/App.js
to use class-based components:
Update src/AppWithRouterAccess.jsx
to use class-based components:
Start your app
Finally, start your app:
Conclusion
You have now successfully authenticated with Okta. With a user's id_token
, you have basic claims for the user's identity. You can extend the set of claims by modifying the scopes
to retrieve custom information about the user. This includes locale
, address
, groups
, and more.