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 Vue app with Okta by performing these steps:
Note: This guide is for @okta/okta-auth-js
>= v5.0.0 and < 6.0.0, vue
3.
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:8080/login/callback |
Logout redirect URIs | http://localhost:8080/ |
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 Vue app
To quickly create a Vue app, Okta recommends the Vue CLI. Follow their guide (opens new window) or use the following steps.
Install dependencies
A simple way to add authentication to a Vue app is using the Okta Auth JavaScript SDK (auth.js). You can install it through npm
:
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/components/About.vue
with the following HTML:
Create src/components/Dashboard.vue
. This page can only be viewed by authenticated users.
Create a src/auth.js
file:
Replace {yourOktaDomain}
with your Okta domain in the previous code example. Replace {clientId}
with the client ID from the app that you created earlier.
Change src/App.vue
to have the following code:
Add a src/components/Login.vue
to render your sign-in form:
To make the v-focus
directive on the email field work, modify app creation code in src/main.js
as follows:
Create routes
Some routes require authentication before rendering. Let's look at what routes are needed for this example:
/
: The default landing page. /about
: A simple about page. /dashboard
: A route that's protected. /login
: The sign-in form. /logout
: A route to sign out a user and redirect them back to the default page.
Create src/router/index.js
with the following code.
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.