npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

sveltekit-session

v0.2.3

Published

A session management library for SvelteKit

Readme

Installation

npm install sveltekit-session

sveltekit-session offers first class support for redis as a session store. In order to use redis, you need to install ioredis.

npm install sveltekit-session ioredis

Quickstart

  1. Create an instance of SessionManager. This example uses the built-in RedisStore.

    /src/lib/server/session.ts

    import { SessionManager } from 'sveltekit-session';
    import RedisStore from 'sveltekit-session/redis';
    import Redis from 'ioredis';
    
    const redisClient = new Redis({ port: 6379, host: 'localhost' });
    
    const redisStore = new RedisStore(redisClient); // pass in the redisClient
    
    //new SessionManager(sessionOptions: SessionOptions, store: Store, cookieOptions?: CookieOptions)
    export const sessionManager = new SessionManager({ ttl: 60 * 60 * 24 * 7, refreshSession: true }, redisStore, { path: '/' });

    See SessionManager Options for more information.

  2. Add the handle hook in /src/hooks.server.ts.

    import type { Handle } from '@sveltejs/kit';
    import { sequence } from '@sveltejs/kit/hooks';
    import { handleSession } from 'sveltekit-session';
    import { sessionManager } from '$lib/server/session';
    
    // your handle hook
    export const myHandle = (async ({ event, resolve }) => {
    	const session = event.locals.session; // session data is ready to be accessed
    	const response = await resolve(event);
    	return response;
    }) satisfies Handle;
    
    export const handle = sequence(handleSession(sessionManager), myHandle); // make sure to add handleSession before any other hooks that make use of the session

    Check out the SvelteKit docs on sequence.

  3. Using SvelteKit-Session is then as simple as this.

    import { sessionManager } from '$lib/server/session';
    
    export const load = async (event) => {
    	// check if session exists
    	if (!event.locals.session) {
    		throw error(401, 'Not logged in');
    	}
    
    	// create session
    	await sessionManager.createSession({ username: 'foo' }, event);
    
    	// use session
    	request.locals.session.email = '[email protected]';
    
    	// destroy session
    	request.locals.session.destroy();
    
    	// get a list of all sessionIds
    	await sessionManager.listSessions();
    
    	// remove specific sessionId from store
    	// THIS WILL NOT DELETE THE COOKIE. USE session.destroy() INSTEAD
    	await sessionManager.deleteSession(sessionId);
    
    	// remove all sessionIds from store
    	await sessionManager.deleteAllSessions();
    };

Typing your session

Import the Session type and add it to the App.Locals interface, then pass your type to the Session type.

/src/app.d.ts

type MySessionData = {
	username: string;
	email?: string;
};

declare namespace App {
	interface Locals {
		session?: import('sveltekit-session').Session<MySessionData>;
	}
}

Custom stores

Custom stores can be easily created by implementing the Store interface.

Check out the RedisStore for an example.

interface Store {
	set(key: string, value: any, ttl?: number): Promise<void>;
	get(key: string): Promise<unknown>;
	update(key: string, value: any, ttl?: number): Promise<void>;
	delete(key: string): Promise<void>;
	clear(): Promise<void>;
	keys(): Promise<string[]>;
}

SessionManager options

type SessionOptions = {
	/**
	 * Number of seconds until the session expires.
	 */
	ttl: number;
	/** @defaultValue `true`
	 * If true, the session will be refreshed on every request.
	 */
	refreshSession?: boolean;
	/** @defaultValue `sessionId`
	 * The name of the session cookie
	 */
	name?: string;
};

type CookieOptions = Omit<CookieSerializeOptions, 'expires' | 'maxAge'>; // expires and maxAge are automatically set based on the ttl
// Take a look at the @types/cookie package for more information on CookieSerializeOptions
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/cookie/index.d.ts#L14