How to do authentication in Svelte using Stores?

Ashutosh Kukreti

User authentication is one of the crucial features of every web application.

Let's pen down the tasks for auth implementation.

  1. Create the Svelte application
  2. Login Component to load the login form.
  3. Post request to submit the data to the server. (We'll fake this)
  4. Action in the Component to handle the login request.
  5. Load the Component with the login success message.
  6. Prevent unauthorized access to the Component.

Now it's time to create the svelte application.

In this article, we'll use SvelteKit to create the Project Skeleton. SvelteKit provides out of the box features like backend, which is missing in Svelte.  It also adds routing ability to the Svelte applications.

npm init svelte@next SvelteAuth

# Which Svelte app template? --> Skeleton project
# Use TypeScript? --> No
# Add ESLint for code linting? --> Yes
# Add Prettier for code formatting? --> Yes

cd SvelteAuth
yarn install

yarn dev

It'll load the application. Please refer the below screenshot

SvelteKit Initial Page Load

In this application, we'll use Bootstrap 5 to create the layout of our app.

Open the file app.html and modify the contents as

<!DOCTYPE html>
<html lang="en">
		<meta charset="utf-8" />
		<meta name="description" content="" />
		<link rel="icon" href="%svelte.assets%/favicon.png" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<link href="" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
		<script src="" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

Let's create a hook /hooks/auth.js under the src folder.

import { writable } from 'svelte/store';

const user = ''
const pass = '123456'

export const store = writable(null);

let sessions = []

export const getUserDetails = async ( username, password ) => {
	if ( username === user && password === pass )
		return 1

In this article, we are not going to implement the API from the backend. But we'll fake the server response.

Create a file LoginComponent.svelte under the /src/Components. You need to create the Components folder.

	import { getUserDetails } from '../hooks/auth';
	import { store } from '../hooks/auth';

	let username = '';
	let password = '';
	let error = ''

	async function login() {
		const user = await getUserDetails( username, password )

		if ( user ) {
			$store = user
			if ( error ) error = ''
		else {
			error = 'Incorrect username and password.'
			console.log("Incorrect username and password.")



<form on:submit|preventDefault={login} class="flex mx-auto col-6">

	<div class="mb-3">
		<label for="username" class="form-label">Username</label>
		<input type="email" class="form-control" id="username" bind:value={username} />

	<div class="mb-3">
		<label for="password" class="form-label">Password</label>
		<input type="password" class="form-control" id="password" bind:value={password} />

	<button type="submit" class="btn btn-primary">Submit</button>
	<div id="error_message" class="text-danger">


And then create a new file login.svelte under the routes directory

	import LoginComponent from '../Components/LoginComponent.svelte';

<LoginComponent />

And now try to load the page http://localhost:3000/login and hit the Submit button. You'll see the page similar to the screenshot

It looks correct because we haven't entered the correct username and password.

We also create one more Component, AfterLogin.svelte under the Components directory.

	User is logged in.

It's is very simple file that we'll load once the user successfully logged in to the system.

Lets create one more file under the routes i.e. dashboard.svelte

	import AfterLogin from '../Components/AfterLogin.svelte';
	import { store } from '../hooks/auth';
	import LoginComponent from '../Components/LoginComponent.svelte';


{#if $store != null }
	<AfterLogin />
{:else }
	<LoginComponent />

We import the writable store from the auth.js.

That's all. It will work just like below gif :). Happy Svelting

Svelte Auth using stores

We forgot to implement logout here.

Create a new file logout.svelte under the routes.

	import { store } from '../hooks/auth';
	$store = null;

<p> User logged Out from the system.</p>

We set the store value to null. And user will be logged out from the system.

In this article, we learn how to implement the Authentication in Svelte using stores. In further articles, we learn how to do Auth using localstorage.