Use the Vue Router (opens new window) and the Okta Vue SDK (opens new window) libraries to simplify your component and route definitions. Some routes require authentication in order to render and others don't. The following are some basic routes that you need to configure for your app:
Default page route To create the default /index
page, update the src/App.vue
file to provide links to relevant locations in your app. You need to provide a Login
link to render the Sign-In Widget, a Logout
link to sign-out of your authenticated session, and links to authenticated pages by using the authState
object (see authStateManager
in the Auth JS SDK (opens new window) ).
You can use the following condition to hide or show specific elements:
v-if="authState && !authState.isAuthenticated"
See the following src/App.vue
file example:
Login route This route hosts the Sign-In Widget and redirects if the user is already signed in. See the Login
route component, specified in the src/components/Login.vue
file, from Create a Sign-In Widget container .
Callback route The Okta Vue SDK (opens new window) provides the LoginCallback (opens new window) component for the callback route. It handles token parsing, token storage, and redirecting to a protected page after a user is authenticated.
See how the callback route component is called from the route definition file (src/router/index.js
) in the Connect the routes section.
Protected route Create a protected route that is only available to users with a valid accessToken
:
You can provide access to a protected route when the requiresAuth
metadata is added in the route definition .
This snippet from the src/router/index.js
router file provides access to the /profile
component only if requiresAuth
metadata is true:
You can use the authState.isAuthenticated
property in the default route to determine if you need to show a protected element.
This snippet from the App.vue
file provides access to the /profile
component only if authState.isAuthenticated
is true:
Note : The authState.isAuthenticated
property is true if both accessToken
and idToken
are valid.
In this protected /profile
component example, the src/components/Profile.vue
file is created to show basic information from the ID token. Retrieve ID token information using the $auth
(opens new window) instance from the Okta Vue SDK and calling the $auth.getUser()
(opens new window) function from the Okta Auth JS SDK.
Note : You can extend the set of claims by modifying the Sign-In Widget scopes (opens new window) settings to retrieve custom information about the user. This includes locale
, address
, groups
, and more (opens new window) .
Connect the routes This route definition example uses the Vue Router (opens new window) and the Okta Vue SDK (opens new window) in the src/router/index.js
file. Notice that the requiresAuth
metadata must be true for protected routes.
Note The Okta Vue SDK (opens new window) provides navigation guard logic to circumvent navigational guard mixins issue in vue-router-next
.
Start your app To run and test your app, execute:
Open a browser and navigate to your app URL. Try to sign in to your app with an existing user from your Okta org.