Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bhasher/svelte-auth

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bhasher/svelte-auth

Svelte-auth is an opinionated svelte-kit library to handle auth easily. It incorporates an existing database architecture. It requires svelte 5 and sqlite/[libsql](https://github.com/tursodatabase/libsql) with [drizzle-orm](https://github.com/drizzle-team

  • 1.1.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
342
increased by3700%
Maintainers
0
Weekly downloads
 
Created
Source

Svelte-auth

Svelte-auth is an opinionated svelte-kit library to handle auth easily. It incorporates an existing database architecture. It requires svelte 5 and sqlite/libsql with drizzle-orm.

Features

  • email-password authentication
  • passkey authentication (with email for registration)
  • sessions management

Planned features

  • MFA
  • Backup codes

Usage

Initialization

The authentication must be initialized somewhere and be accessible to the rest of the back-end. It's also possible to configure the name of session's cookies and passkeys' settings.

# auth.ts

import { Auth } from '@bhasher/svelte-auth';
import { db } from '$lib/server/db';
import * as types from '$lib/server/db/schema';

export default new Auth(db, types, 'session', { 
	name: 'my-app',
	id: 'localhost',
	origin: 'http://localhost:5173',
    requireUserVerification: false  # Optional
});

Schemes

Each type has two functions to simplify database setup: getSqliteColumnsMap(...) and getSqliteColumnsConfig.

The databases schemes can then be initialized very easily, depending on the needs. For technical limitations, the schemes cannot be renamed, and no mandatory fields can be added.

schema.ts

export const users = sqliteTable('users', User.getSqliteColumnsMap(), User.getSqliteColumnsConfig);
export const sessions = sqliteTable( 'sessions', Session.getSqliteColumnsMap(users), Session.getSqliteColumnsConfig);

# Optional
export const passwords = sqliteTable( 'passwords', Password.getSqliteColumnsMap(users), Password.getSqliteColumnsConfig);

# Optional
export const passkeys = sqliteTable( 'passkeys', Passkey.getSqliteColumnsMap(users), Passkey.getSqliteColumnsConfig);

It's also possible to define relations.

export const usersRelations = relations(users, ({ many, one }) => ({
    ...
    password: one(passwords, {
        fields: [users.id],
        references: [passwords.userId]
    }),
    passkeys: many(passkeys)
}));

export const passskeysRelations = relations(passkeys, ({ one }) => ({
	user: one(users, {
		fields: [passkeys.userId],
		references: [users.id]
	})
}));

Flows

Registration with email and password
const userId = await auth.registerUser(email);
await auth.registerPassword(userId, password);
# Optional
await auth.initSession(cookies, userId);
Registration with email and passkey
# Step 1
return await auth.getRegisterPasskeyOptions(email));

# Step 2
const passkey = await auth.verifyRegisterPasskeyResponse(email, response);
const userId = await auth.registerUser(email);
await auth.savePasskey(passkey, userId);
# Optional
await auth.initSession(cookies, userId);
Login with email and password
const userId = await auth.loginPassword(email, password);
# Optional
await auth.initSession(cookies, userId);
Login with passkey
# Step 1
return await auth.getLoginPasskeyOptions();

#Step 2
const userId = await auth.verifyLoginPasskeyResponse(rid, response);
#Optional
await auth.initSession(cookies, userId);

Components

To use the tailwind of the library, you have to edit the tailwind.config.ts:

export default {
	content: [
        ...
        './node_modules/@bhasher/**/*.{html,js,svelte,ts}'
    ],
}

FAQs

Package last updated on 13 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc