Integrate Hanko with JavaScript
Learn how to quickly add authentication and user profile in your vanilla Javascript app using Hanko.
Add the Hanko API URL
Retrieve the API URL from the Hanko console and place it in your .env
file.
If you are self-hosting, you need to provide the URL of your running Hanko backend.
Add <hanko-auth>
component
The <hanko-auth>
web component adds a login interface to your app. Begin by importing the register
function from @teamhanko/hanko-elements
. Call it with the Hanko API URL as an argument to register <hanko-auth>
with the browser’s CustomElementRegistry. Once done, use it in your markup.
Define event callbacks
The Hanko client from @teamhanko/hanko-elements
lets you “listen” for specific events. It simplifies the process of subscribing to events, such as user logins.
By now, your sign-up and sign-in features should be working. You should see an interface similar to this 👇
Add <hanko-profile>
The <hanko-profile>
component provides an interface, where users can manage their email addresses and passkeys.
It should look like this 👇
Implement logout functionality
Use the Hanko client provided by @teamhanko/hanko-elements
to log out users. On logout a custom event is
dispatched that you can subscribe to:
Customize component styles
The styles of the hanko-auth
and hanko-profile
elements can be customized using CSS variables and parts. See our
guide on customization here.
Authenticate backend requests
To authenticate requests on your backend with Hanko, refer to our backend guide.