Create routes
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 to render and other routes 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. Then, provide a link to your sign-in page, a link to sign-out of your authenticated session, and links to authenticated pages by using the authState
object (see authStateManager
in 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 directs the user to your app sign-in page. See the Login
route component, specified in the src/components/Login.vue
file, from Create the sign-in component.
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.
For example, this snippet from the src/router/index.js
router file provides access to the /dashboard
component only if the requiresAuth
metadata is true:
You can use the authState.isAuthenticated
property in the default route landing page to determine if you need to show a protected element.
For example, this snippet from the App.vue
file provides access to the /dashboard
component only if authState.isAuthenticated
is true:
Note: The authState.isAuthenticated
property is true if both accessToken
and idToken
are valid.
In this protected /dashboard
component example, the src/components/Dashboard.vue
file is created to show basic information from the ID token. Retrieve ID token information by 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 scope settings in your Okta configuration 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 the navigational guard mixins issue in vue-router-next
.
Start your app
To run and test your app, execute:
Open a browser and go to your app URL. Try to sign in to your app with an existing user from your org.