sveltekit-jwt Svelte Themes

Sveltekit Jwt

impl JsonWebToken for SvelteKit.

SvelteKit JWT

impl JsonWebToken for SvelteKit.

pnpm i -D sveltekit-jwt

Features

  • Support Asymmetric and Symmetric signature.
  • Auto cache remote JWKs (jku).

Usage

import { checkout } from "sveltekit-jwt";
import { env } from "$env/dynamic/private";
import type { Handle, RequestEvent } from "@sveltejs/kit";

export const handle: Handle = async ({ event, resolve }) => {
    // Check the expiration and signature of the token.
    const payload = await checkout(event, env.JWT_SECRET);
    if (payload) {
        // You may want to check if the payload is valid using zod or something.
        event.locals.token = payload;
    }

    return resolve(event);
};

Verify Asymmetric Signature

If you are using asymmetric signature (RSA or ECDSA) with jku header provided, you don't need to pass the secret key.

import { checkout } from "sveltekit-jwt";
import type { Handle, RequestEvent } from "@sveltejs/kit";

export const handle: Handle = async ({ event, resolve }) => {
    const payload = await checkout(event);
    if (payload) {
        event.locals.token = payload;
    }

    return resolve(event);
};

Developing

Once you've installed dependencies with pnpm install, start a development server:

pnpm dev

Everything inside src/lib is part of the library, everything inside src/routes is used as a showcase or preview app.

Building

To build the library:

pnpm package

To create a production version of the showcase app:

pnpm build

You can preview the production build with pnpm preview.

To deploy the app, you may need to install an adapter for the target environment.

Publishing

To publish the library to npm:

pnpm publish

Top categories

Loading Svelte Themes