Integrate Hanko with Vue
Learn how to quickly add authentication and user profile in your Vue app using Hanko.
Create a Vue application
Run the following command to create a new Vue application with vite:
Install @teamhanko/hanko-elements
Once you’ve initialized your Vue app, installing hanko-elements
provides you with access to the prebuilt components: hanko-auth
and hanko-profile
.
We will also install vue-router
to setup our routes and pages with.
Setup your Hanko project
Go to the Hanko console and create a project for this application.
During creation make sure to input the URL you will be developing on as the APP URL
.
(Most likely http://localhost:5173/)
Add the Hanko API URL
Retrieve your API URL from the Hanko console, and paste this in a .env
file.
If you are self-hosting you need to provide the URL of your running Hanko backend.
Configure component resolution
Vue needs to know which elements to treat as custom elements, otherwise it will issue a warning regarding component
resolution. To do so, provide a predicate function that determines which elements are to be considered custom elements
to compilerOptions.isCustomElement
in your configuration.
To do this update your vite.config.js
file to:
Setup TS support for Vue components
TypeScript doesn’t understand .vue files by default, which can cause IDE errors or issues during development.
To fix this we will create a new file shims-vue.d.ts
in our src/
directory.
Add the following content to make TS understand .vue files:
Create Hanko components
Create a folder called components
and create two in it, HankoAuth.vue
and HankoProfile.vue
.
Hanko Auth
Now lets setup the HankoAuth.vue
file to create a functioning login component.
Here we subscribe to the onSessionCreated
event, this triggers when a user successfully logs in. You can use these event to perform any desired action (e.g. redirect to your dashboard).
For more information please refer to the Auth Component Page.
Hanko profile
After setting up the HankoAuth let’s set up the HankoProfile.vue
file to create a interface where users can.
manage their Email Addresses
and credentials.
For more information please refer to the Profile Component Page.
Setup routes and views
To route our app we will use the RouterView
component from the vue-router.
First let’s set up our views which will be used by the router.
Create Views
Create two files, HomeView.vue
and DashboardView.vue
in a new directory /views
.
These Views will be the content that will be shown on a page.
Setup your views like this:
And:
Setup Router
Create a file index.ts
in a new directory /router
.
In here we can set up the vue-router
just like this:
Add the <RouterView>
component to your App.vue
file like this:
Finally intergrate the router with the app in main.ts
just like this:
By now you should be able to go to /
to see the <HankoAuth>
, and to /dashboard
to see the <HankoProfile>
.
They should look something like this👇
If HankoAuth
is not loading please restart the application as it might’ve not loaded the .env correctly the first time.
HankoProfile
will only look like the picture while you are logged in.
Implement logout functionality
You can use @teamhanko/hanko-elements
to easily logout users. Here we will make a logout button.
Create LogoutButton.vue
and insert the code below.
Lets add this component to our DashboardView
just like this:
Customize component styles
You can customize the appearance of hanko-auth
and hanko-profile
components using CSS variables and parts. Refer to our customization guide.
Securing routes
To secure our routes we should validate the session token at the backend. Please refer to our backend guides.
To do this we can implement middleware to our router/index.ts
file.
For this example we created a isUserAuthenticated
function which validates our cookie using our Backend.
Here we use router.beforeEach
to validate our route if our route is added to the secureRoutes variable:
To verify that it works, Logout on your app and go to /dashboard, you should get redirected back!
Try it yourself
Vue example
It uses Express.js for the backend, full source code available on our GitHub.