0.2.1 • Published 11 months ago

sveltekit-jwt v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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
0.2.1

11 months ago

0.2.0

11 months ago

0.1.0

12 months ago

0.0.0

12 months ago