Vue
In this guide you will learn how to use the hanko-elements web components to add authentication and a user profile to your Vue application.
Install dependencies
Install the @teamhanko/hanko-elements
package:
- npm
- Yarn
npm install @teamhanko/hanko-elements
yarn add @teamhanko/hanko-elements
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:
- Vite Config
- Vue CLI Config
import vue from '@vitejs/plugin-vue'
export default {
plugins: [
vue({
template: {
compilerOptions: {
isCustomElement: (tag) => tag.startsWith("hanko-")
}
}
})
]
}
module.exports = {
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
.tap(options => ({
...options,
compilerOptions: {
isCustomElement: (tag) => tag.startsWith("hanko-")
}
}))
}
}
Add <hanko-auth>
component
To provide a login interface in your app, use the <hanko-auth>
web component. To do so, first import the
register
function from @teamhanko/hanko-elements
in your Vue component. Then call register
to register the
<hanko-auth>
element with the browser's
CustomElementRegistry
and use the
element in your component template.
When adding the <hanko-auth>
element to your template you must provide the URL of the Hanko API via the api
attribute. If you are using Hanko Cloud, you can find the API URL on your project dashboard.
If you are self-hosting you need to provide the URL of your running Hanko backend.
<script setup>
import { onMounted } from "vue";
import { register } from "@teamhanko/hanko-elements";
const hankoApi = "<YOUR_API_URL>";
onMounted(() => {
// register the component
// see: https://github.com/teamhanko/hanko/blob/main/frontend/elements/README.md#script
register({ shadow: true })
.catch((error) => {
// handle error
});
});
</script>
<template>
<hanko-auth :api="hankoApi" />
</template>
Define login callbacks
The <hanko-auth>
element dispatches a custom hankoAuthSuccess
event on successful login. React to this
event in order to, for example, redirect your users to protected pages in your application,
e.g. a user profile page.
To do so, you can use Vue's v-on
directive (shorthand: @
) and supply a callback directly on the <hanko-auth>
element:
<script setup>
import { useRouter } from "vue-router";
import { onMounted } from "vue";
import { register } from "@teamhanko/hanko-elements";
const hankoApi = "<YOUR_API_URL>";
onMounted(() => {
// register the component
// see: https://github.com/teamhanko/hanko/blob/main/frontend/elements/README.md#script
register({ shadow: true })
.catch((error) => {
// handle error
});
});
const router = useRouter();
const redirectAfterLogin = () => {
// successfully logged in, redirect to a page in your application
router.push({ path: "..." });
};
</script>
<template>
<hanko-auth @hankoAuthSuccess="redirectAfterLogin" :api="hankoApi" />
</template>
Add <hanko-profile>
component
To provide a page where users can manage their email addresses, password and passkeys, use the <hanko-profile>
web
component. Just as with the <hanko-auth>
component, import the register
function from @teamhanko/hanko-elements
in
your Vue component. Then call register
to register the <hanko-profile>
element with the browser's
CustomElementRegistry
and use
the element in your component.
When adding the <hanko-profile>
element to your template you must provide the URL of the Hanko API via the api
attribute. If you are using Hanko Cloud, you can find the API URL on your project dashboard.
If you are self-hosting you need to provide the URL of your running Hanko backend.
<script setup>
import { onMounted } from "vue";
import { register } from "@teamhanko/hanko-elements";
const hankoApi = "<YOUR_API_URL>";
onMounted(() => {
// register the component
// see: https://github.com/teamhanko/hanko/blob/main/frontend/elements/README.md#script
register({ shadow: true })
.catch((error) => {
// handle error
});
});
</script>
<template>
<hanko-profile :api="hankoApi" />
</template>
Customize component styles
The styles of the hanko-auth
and hanko-profile
can be customized using CSS variables and parts. See our guide
on customization here.
Authenticate backend requests
If you want to authenticate requests in your own backend, please view our backend guide.