On this page
Sign users in to your web app using the redirect model
Add a user sign-in flow to a server-side web app with Okta's redirect model (opens new window).
Learning outcomes
- Implement a simple redirect to an Okta-hosted sign-in page.
- Configure a server-side web app to use Okta.
- Test that users can sign in and sign out.
- Define which parts of an app require authentication and which don't.
What you need
Sample code
Overview
The easiest and most secure way to add a user sign-in flow to your server-side web app is to use an Okta-hosted Sign-In Widget. When a user attempts to sign in, the app redirects them to the widget hosted on an Okta web page. After they've signed in successfully, Okta redirects them back to the app. This is known as the redirect authentication deployment model.
Note: To use the redirect model in a single-page application (SPA), see Sign users in to your SPA using the redirect model. To use the redirect model in a mobile app, see Sign users in to your mobile app using the redirect model.
In this quickstart, you:
- Create an app integration in the Admin Console.
- Create and configure a new web app to use Okta.
- Test that a user can sign in and sign out.
- Configure different levels of access for specific areas of the site.
Tip: You need your Okta org domain to follow this tutorial. It looks like
dev-123456.okta.com
. See Find your Okta domain. Where you see{yourOktaDomain}
in this guide, replace it with your Okta domain.
Create an app integration in the Admin Console
An app integration represents your app in your Okta org. Use it to configure how your app connects with Okta services.
To create an app integration for your app:
Open the Admin Console for your org.
- Sign in to your Okta organization (opens new window) with your administrator account.
- Click Admin in the upper-right corner of the page.
Go to Applications > Applications to view the current app integrations.
Click Create App Integration.
Select OIDC - OpenID Connect as the Sign-in method.
Select Web Application as the Application type, then click Next.
Note: You can break the sign-in or sign-out flows for your app if you choose the wrong app type.
Enter an App integration name. For example, My first web application.
Enter the callback URLs for the local development of your app.
- Enter
- Enter
Note: The values suggested here are those used in the sample app.
- Enter
Select Allow everyone in your organization to access for Controlled access.
Click Save to create the app integration.
The configuration page for the new app integration appears. Keep this page open.
Note: For a complete guide to all the options not explained in this guide, see Create OIDC app integrations (opens new window).
Note your client ID and client secret
Make a note of two values that you use to configure your web app. Both are in the configuration pane for the app integration that you've created:
- Client ID: Found on the General tab in the Client Credentials section.
- Client Secret: Found on the General tab in the Client Credentials section.
Moving on, where you see {clientId}
and {clientSecret}
in this guide, replace them with your client ID and client secret.
Create and configure a new web app to use Okta
Now that you have created the app integration and noted the configuration settings, complete the following steps:
- Create a web app.
- Add the required packages to your app
- Configure your app to use Okta
- Add the pages and logic for a user to sign in and sign out
Create a web app
Add the required packages to your app
Configure your app to use Okta
Earlier you noted the client ID and client secret values generated for your app integration. Add these and your Okta domain to your app's configuration.
Add the pages and logic for a user to sign in and sign out
A user can start the sign-in process by:
- Clicking a sign-in link or button
- Trying to access a protected page, such as their profile page.
In both cases, the app redirects the browser to the Okta-hosted sign-in page. See Redirect to the sign-in page.
After the user signs in, Okta redirects the browser to the sign-in redirect URI that you entered earlier. Similarly, after a user signs out, Okta redirects the browser to the sign-out redirect URI. Both sign-in and sign-out redirect URIs are called callback routes. Users don't see callback routes, and they aren't the user's final destination. However, your app does need to implement them. See Define a callback route.
After the user signs in, Okta returns some of their profile information to your app. The default profile items (called claims) returned by Okta include the user's email address, name, and preferred username. These are sent in an ID token (opens new window) as part of the redirect to the sign-in redirect URL. See Get the user's information.
Redirect to the sign-in page
Note: To customize the Okta sign-in form, see Style the Okta-hosted Sign-In Widget.
Define a callback route
Get the user's information
The user information that Okta returns in an ID token after a user has signed in is saved automatically in req.user
. For example, you can see all the user's claims in user.claims
. In this section, you create a simple profile page that lists all the claims returned.
Create a template file called views > profile.pug.
Replace the contents of the new file with the following code:
extends layout block content h2= "My Profile" br div p Hello, #{user.displayName}. br table thead t th Claim th Value tbody for values, claims in user tr td #{claims} td #{values}
Open app.js.
Add the route handler for the profile page:
app.use('/profile', (req, res) => { res.render('profile', { user: req.user }); });
Note: The claims that you see may differ depending on the scopes requested by your app. See Configure your app to use Okta and Scopes (opens new window).
Test that a user can sign in and sign out
Your site now has enough content to sign a user in with Okta, prove they've signed in, and sign them out. Test it by starting your server and signing a user in.
Run the following command to start your app:
npm start
Open a browser and go to
http://localhost:3000
.Click Sign In. The browser redirects you to Okta to sign in using the Sign-In Widget.
After you've signed in, you're redirected to the profile page. This displays the user information returned by Okta in the ID token.
Click Sign Out. The browser returns you to the home page.
Note: If you're signed in as an administrator in the same browser already, it displays your name. You can open an incognito window and create a test user in the Admin Console to use.
Configure different levels of access for specific areas of the site
Your app can require authentication for the entire site or just for specific routes. Routes that don't require authentication are accessible without signing in, which is also called anonymous access.
Require authentication for everything
Some apps require user authentication for all routes, for example a company intranet.
You can use a middleware function to protect every endpoint so that only authenticated users have access.
Open app.js.
Add the middleware function:
function ensureSignedIn(req, res, next) { if (req.isAuthenticated()) { return next(); } res.redirect('/signin') }
Add the following line into the view engine setup to enforce authentication.
app.use(ensureSignedIn());
Require authentication for a specific route
Your website may have a protected portion that is only available to authenticated users.
You can use the same middleware function to require authentication for everything to protect only specific routes. Add it as a new parameter to the route handler when required:
app.use('/profile', ensureSignedIn, (req, res) => {
res.render('profile', { user: req.user });
});
Allow anonymous access
Your website may enable anonymous access for some content but require a user to sign in for other content or to take some other action. For example, an ecommerce site might allow a user to browse anonymously and add items to a cart, but require a user to sign in for checkout and payment.
To handle anonymous access, only add the above middleware to the specific routes that require authentication.
Next steps
- Protect your API endpoints.
- Custom domain and email address
- Style the Okta-hosted Sign-In Widget.
- Sign users in to your mobile app using the redirect model
- Multi-tenant solutions
Okta Developer Blog:
- Simple Multi-Factor Authentication in Node (opens new window)
- Tutorial: Build a Basic CRUD App with Node.js (opens new window)
- Quickly Build Node.js Apps with Sails.js (opens new window)
- Build Secure Node Authentication with Passport.js and OpenID Connect (opens new window)
- Build a NodeJS App with TypeScript (opens new window)