This tutorial shows how to use PlusAuth with Vue 3 Single Page Application. If you do not have a PlusAuth account, register from here.
Create PlusAuth Client
After you sign up or log in to PlusAuth, you need to create a client to get the necessary configuration keys in the dashboard. Go to Clients and create a client with the type of Single Page Application
Configure Client
Get Client Properties
You will need your Client Id
for interacting with PlusAuth. You can retrieve it from the created client's details.
Configure Redirect and Logout URIs
When PlusAuth authenticates a user, it needs a URI to redirect back with access and id token. That URI must be in your client's Redirect URI
list. If your application uses a redirect URI which is not white-listed in your PlusAuth Client, you will receive an error.
The same thing applies to the logout URIs. After the user logs out, you need a URI to be redirected.
Create a Vue Application
Follow Quick Start Guide from Vue.js official document site and create a project. Make sure you have answered to Add Vue Router for Single Page Application development?
prompt as Yes
. Other prompts are not required for this tutorial.
Here is the overview of the commands:
✔Project name: … <your-project-name>
✔Add TypeScript? … No / Yes
✔Add JSX Support? … No / Yes
✔Add Vue Router for Single Page Application development? … No / Yes
✔Add Pinia for state management? … No / Yes
✔Add Vitest for Unit testing? … No / Yes
✔Add an End-to-End Testing Solution? … No / Cypress / Playwright
✔Add ESLint for code quality? … No / Yes
✔Add Prettier for code formatting? … No / Yes
Scaffolding project in ./<your-project-name>...
Done.
Install OIDC Client
For interacting with PlusAuth it is advised to use an OpenID Connect library. In this tutorial we will be using oidc-client-js but you could use any OpenID Connect library.
Install oidc-client-js
with the following command
Configure Vue Application to use PlusAuth
We will be using dotenv
files for maintaining providing some constant values. Vite supports them, so no need for extra configuration.
Create the .env file
Create the .env
file at the root of your project with the following and modify values accordingly.
Configure OIDC Client
We need to initialize our OIDC Client library to handle authentication-related operations. Create auth.js
in src
folder. Configure oidc-client-js
as following:
You may have noticed that the values defined in the Configure Client section are used here. If you have used different values make sure to update this file accordingly.
Configure Vue Application
Let's start by defining our application's entry point file. Go to file named main.js
in the src
folder. Import the auth
file we have created above and make the following changes.
Configure Router
Now let's define our application's router. We are going to define routes of our views. requiresAuth
flag in the metadata of routes will ensure those routes are accessible only by authenticaed users.
Create router.js
in src
folder as following:
Implement login, user profile, and logout
Until now, we have defined our authentication helper and routes. It is time to create the pages and interact with auth helper.
Create Main Vue Component
Let's create a simple layout for our application. Add Header
component and router-view
to App.vue
.
Create Header Component
Create Header.vue
under src/components
folder. It will be a basic header. If a user is authenticated, it will show the user's identifier and a Logout
button. If not, a Login
button will be there to initiate login.
Create AuthCallback
To handle authorization results after a successful login, we need a simple page and let the library handle the authentication result. Create AuthCallback.vue
under src/components
folder.
Create SilentRenew
Access tokens retrieved from PlusAuth have a life span. oidc-client-js
automatically provides access_token
renewal without too much hassle. Before your access token expires, it will receive a new one in the background so that your users will have a flawless app experience without signing in again.
Create SilentRenew.vue
under src/components
folder as following:
Create Views
HomePage
Create Home.vue
under src/components
.
Profile Page
Create Profile.vue
under src/components
.
Add Unauthorized Page
We will display a page whenever a user tries to access a protected route without signing in.
Create Unauthorized.vue
under src/components
.
See it in action
That's it. Start your app and point your browser to http://localhost:5173. Follow the Log In link to log in or sign up to your PlusAuth tenant. Upon successful login or signup, you should be redirected back to the application.