Svault is an authentication, authorization, and user management library for Svelte/SvelteKit applications. Svault supports both native username/password authentication as well as OAuth.
๐ Website | ๐ Documentation | โจ๏ธ Blog | ๐ผ LinkedIn
npm install svault
When registering your application for OAuth set your callback url to match this format /oauth/[provider]/validate
.
In the src
directory of your project, create a hooks.server.ts
file and paste the following code:
```TypeScript
// Import if you would like to use Oauth login (necessary for all Oauth)
import { SvaultOauth } from 'svault';
// Import if you would like to use Github Oauth import { github } from 'svault'; import { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } from '$env/static/private';
// Import if you would like to use Google Oauth import { google } from 'svault'; import { GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET } from '$env/static/private'; // Google callback urls have to match the callback url you setup in your development app so paste it here to pass into the Oauth function const googleCallback = 'http://localhost:5173/oauth/google/validate';
// Import if you would like to use Discord Oauth import { discord } from 'svault'; import { DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET } from "$env/static/private"; // Discord callback urls have to match the callback url you setup in your development app so paste it here to pass into the Oauth function const discordCallback = 'http://localhost:5173/oauth/discord/validate';
// Set redirect path to your desired endpoint upon user login const redirectPath = '/(yourPathHere)'; //ex. 'const redirectPath = '/redirectPage'
// Place the Oauth providers here const providers = [ github(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, redirectPath), google(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, redirectPath, googleCallback), discord(DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET, redirectPath, discordCallback) ];
// Svault Oauth handler export const handle = SvaultOauth({ providers });
3. Create an `.env` file at the root level of your project and define your client ID and secret as variables. Example:
```TypeScript
// Paste if using Discord Oauth
DISCORD_CLIENT_ID = YOURIDHERE
DISCORD_CLIENT_SECRET = YOURSECRETHERE
// Paste if using Github Oauth
GITHUB_CLIENT_ID = YOURIDHERE
GITHUB_CLIENT_SECRET = YOURSECRETHERE
// Paste if using Google Oauth
GOOGLE_CLIENT_ID = YOURIDHERE
GOOGLE_CLIENT_SECRET = YOURSECRETHERE
In your +page.svelte
file that has your login page:
/oauth/[provider-name-here]/auth
// Example +page.svelte file for github authentication
<button>
<a href="/oauth/github/auth">Github</a>
</button>
<button>
<a href="/oauth/google/auth">Google</a>
</button>
<button>
<a href="/oauth/discord/auth">Discord</a>
</button>
Note: OAuth providers are not set up to store any data in a database in this current iteration of Svault.
src
directory, create a hooks.server.ts
file
```TypeScript
// Import if you would like to utilize native user registration and login
import { SvaultNative } from 'svault';// Set redirect path to your desired endpoint upon user login const redirectPath = '/[yourPathHere]'; //ex. 'const redirectPath = '/homepage'
// Svault native handler export const handle = SvaultNative(redirectPath);
2. Create a PostgreSQL table with the following columns:
```SQL
CREATE TABLE [YOURTABLENAME] (
username VARCHAR NOT NULL,
password VARCHAR NOT NULL
)
routes
directory, create a route called login
. In your login +page.svelte
, create form elements for taking in username and password inputs. Create buttons for login and register.registerValidate
and loginValidate
logout
endpoint
```TypeScript
// Example routes/login/+page.svelte//MAX_AGE will determine the expiration time of the user's session MAX_AGE = Date.now() + {someNumberHere} * {someNumberHere} // ex. Date.now() + 1000 * 60 * 60 --> session will last for 1 hour
5. After submitting the form, the user will be redirected to the endpoint of your choice.
- Upon registering, the user will be added to the database with the username and a secure hashed password.
- On login, the user will be authenticated through your database.
- A browser cookie will be created as well as a session in local memory storage called "svault-auth".
<div align="center">
<img src="cookiepicture.png">
</div>
- The session will have an expiration time determined in your `.env` file.
- Sessions will automatically be cleaned and deleted upon expiration.
- On logout, the user will be redirected to the home page, the cookie will be deleted from the browser, and the session will be deleted from local memory store.
6. And you're good to go!
<br>
### Using Both Native Authentication and OAuth
1. To implement native authentication and OAuth, you can use SvelteKit's <a href=https://kit.svelte.dev/docs/modules#sveltejs-kit-hooks-sequence>Sequence</a> helper function.
2. In your existing `hooks.server.ts` file, add the following:
```TypeScript
import { sequence } from '@sveltejs/kit/hooks';
/*
NOTE: CANNOT HAVE 2 HANDLE FUNCTIONS!
export const handle = SvaultNative(redirectPath);
export const handle = SvaultOauth({ providers });
*/
// Rename functions to their corresponding type of authentication
export const native = SvaultNative(redirectPath);
export const oauth = SvaultOauth({ providers });
// Use sveltekit sequence method to run both hooks in sequence
export const handle = sequence(oauth, native);
event.locals
object. This allows you to display any necessary information you would like, client-side.event.locals.username
in your +layout.server.ts
, and then call it on your +page.svelte
.event.locals.failure
object.import type { LayoutServerLoad } from '/$types';
export const load = (async ({ locals }) => { const { username, failure } = locals; return await { username, failure }; }) satisfies LayoutServerLoad;
```TypeScript
// In +page.svelte
<!-- displays username upon successful login -->
{#if data.username}
<a
href="/logout"
data-sveltekit-reload
>
Hello {data.username}, Log out
</a>
{:else}
<!-- displays error message if login is unsuccessful -->
{#if data.failure}
<div>
{data.failure}
</div>
{/if}
Svault is an amazing project with many areas for iteration. Here are some of the ideas to add and improve our features:
Franki Biswas Github | LinkedIn
Tristan Bott Github | LinkedIn
Michael Buenrostro Github | LinkedIn
Michelle Conroy Github | LinkedIn
Inspired by Lucia and Auth.js, Svault expands the authentication tools available to Svelte/SvelteKit developers.
Svault is developed under the MIT license.