Note: This document is only for Okta Classic Engine. If you are using Okta Identity Engine, see Sign in to SPA with embedded Widget. See Identify your Okta solution (opens new window) to determine your Okta version.
This guide will walk you through integrating authentication into a React app with Okta by performing these steps:
This guide is for @okta/okta-signin-widget
v5.7.3, @okta/okta-react
v6.0.0 and @okta/okta-auth-js
v5.1.1.
Prerequisites
If you do not already have a Developer Edition Account, 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 values similar to:
Setting | Value |
App Name | OpenID Connect App |
Login redirect URIs | http://localhost:3000/login/callback |
Logout redirect URIs | http://localhost:3000 |
Allowed grant types | Authorization Code |
Note: It is important to choose the appropriate application type for apps which are public clients. Failing to do so may result in Okta API endpoints attempting to verify an app's client secret, which public clients are not designed to have, 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 quickly create a React app, we recommend using Create React App.
If you need more information, see the Create React App getting started guide (opens new window).
Install Dependencies
To provide a fully-featured and customizable sign-in experience, the Okta Sign-In Widget is available to handle User Lifecycle operations, MFA, and more. You can install it using npm
:
You also need @okta/okta-auth-js
, @okta/okta-react
and react-router-dom
to manage our routes:
Config
Create a src/config.js
file. Make sure to replace the {...}
placeholders with your Okta values.
Note: In Okta Sign-In Widget version 7+, Okta Identity Engine is enabled by default. If you are using version 7+ and want to use Okta Classic Engine rather than Identity Engine, you need to specify useClassicEngine: true
in the configuration options (opens new window) passed into the new OktaSignIn()
call.
To render the Sign-In Widget in React, you must create a wrapper that allows you to treat it as a React component.
Create a src/OktaSignInWidget.js
file:
Create Routes
Some routes require authentication in order to render. Defining those routes is easy using SecureRoute
from @okta/okta-react
. Let's take a 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
: Show the sign-in page. /login/callback
: A route to parse tokens after a redirect.
/ - index page
First, create src/Home.js
to provide links to navigate our app:
/protected
This route will only be visible to users with a valid accessToken
.
Create a new component src/Protected.js
:
/login
This route hosts the Sign-In Widget and redirects if the user is already logged in. If the user is coming from a protected page, they'll be redirected back to the page upon login.
Create a new component src/Login.js
:
/login/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 login.
Connect the Routes
Our example is using react-router-dom
. By default you can include your components and Routes in src/App.js
. If you need access to particular router properties, such as the history
object that's used to override the default sign-in flow, you need to create a wrapper component around <Router>
.
Update src/App.js
to create a Router and call <AppWithRouterAccess
>` as a child component:
Create src/AppWithRouterAccess.js
and 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:
Start your app
Finally, start your app:
Conclusion
You have now successfully authenticated with Okta! Now what? 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.