Skip to main content

Create a SvelteKit application

Create a new SvelteKit application using the official SvelteKit starter. Run the following command to create a new SvelteKit application:

Install @teamhanko/hanko-elements

The Hanko Elements package provides pre-built authentication components (hanko-auth, hanko-profile) that work with Svelte’s reactive system. Install the package:

Set up your Hanko project

Create a Hanko project in the Cloud Console to get your API URL. Go to the Hanko Console and create a project for this application.
Set the APP URL to your development URL (typically http://localhost:5173/ for SvelteKit).

Add your Hanko API URL

Add your Hanko API URL to your environment file. The PUBLIC_ prefix makes it accessible in both server and client-side code in SvelteKit. Retrieve your API URL from the Hanko Console and add it to your .env file:
.env
If you are self-hosting you need to provide the URL of your running Hanko backend.

Create Hanko components

Create a components folder with two files: HankoAuth.svelte and HankoProfile.svelte.

Hanko Auth

Now let’s setup the HankoAuth.tsx file to create a functioning login page. 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.
components/HankoAuth.svelte
Now simply import the component you just created into any page.
routes/+page.svelte
By now, your sign-up and sign-in features should be working. You should see an interface similar to this 👇
sign up

Hanko Profile

After setting up the HankoAuth let’s set up the HankoProfile.jsx file to create an interface where users can manage their Email Addresses and credentials. For more information please refer to the Profile component page.
components/HankoProfile.svelte
Now simply import the component you just created into any page.
routes/dashboard/+page.svelte
It should look something like this 👇
profile page

Implement logout functionality

You can use @teamhanko/hanko-elements to easily logout users. Here we will make a logout button. Create LogoutButton.tsx and insert the code below.
components/LogoutButton.svelte

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 verify the session token in your SvelteKit application, we’re using the session/validate API request. By checking for a valid session token this middleware will ensure secure access to specific routes, like /dashboard and /protected.
The middleware hook extracts and verifies the session token, and redirect unauthorized users back to the home or login page.
For more info on middlewares / hooks in SvelteKit and where to put the hooks.server.ts file,
please refer to SvelteKit Hooks.
What happens here is that during each request the handle function will be called. It then verifies the session token and redirects the user back if he is on a private route and the session token is not valid.
Hooks like these tends to not always work after creating it, if this is the case try restarting your SvelteKit app.
hooks.server.ts

Getting user data

Let’s use the Hanko SDK to get user data. Let’s update the dashboard page to display some of the information from the user.
routes/dashboard/+page.svelte

Try out yourself

SvelteKit example

Full source code available on our GitHub