Skip to main content

Vue

In this guide you will learn how to add authentication to your Vue application using the Hanko custom element.

Install dependencies

Install the @teamhanko/hanko-elements package:

npm install @teamhanko/hanko-elements

Register custom element with Vue

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:

vite.config.js
import vue from '@vitejs/plugin-vue'export default {  plugins: [    vue({      template: {        compilerOptions: {          isCustomElement: (tag) => tag === "hanko-auth"        }      }    })  ]}

Import & use custom element

Import @teamhanko/hanko-elements/hanko-auth in the component where you want to use the Hanko custom element. Doing so registers the <hanko-auth> element with the browser's CustomElementRegistry. Then use the element in your component template:

HankoAuth.vue
<script setup>import "@teamhanko/hanko-elements/hanko-auth";const api = import.meta.env.VITE_HANKO_API;const lang = import.meta.env.VITE_HANKO_LANG;</script><template>  <hanko-auth :api="api" :lang="lang" /></template>

Defining login callbacks

The <hanko-auth> element dispatches a custom success event on successful login. React to this event in order to, for example, redirect your users to protected pages in your application.

To do so, you can use Vue's v-on directive (shorthand: @) and supply a callback directly on the <hanko-auth> element:

HankoAuth.vue
<script setup>import { useRouter } from "vue-router";import "@teamhanko/hanko-elements/hanko-auth";const api = import.meta.env.VITE_HANKO_API;const lang = import.meta.env.VITE_HANKO_LANG;const router = useRouter();const redirectToProfile = () => {  router.push({ path: "/profile" });};</script><template>  <hanko-auth @success="redirectToProfile" :api="api" :lang="lang" /></template>

UI customization

The styles of the hanko-auth element can be customized using CSS variables and parts. See our guide on customization here.

Backend request authentication

If you want to authenticate requests in your own backend, please view our backend guide.