0.2.1 • Published 2 years ago
sveltekit-jwt v0.2.1
SvelteKit JWT
impl JsonWebToken for SvelteKit.
pnpm i -D sveltekit-jwtFeatures
- 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 devEverything 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 packageTo create a production version of the showcase app:
pnpm buildYou 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