impl JsonWebToken for SvelteKit.
pnpm i -D sveltekit-jwt
jku).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);
};
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);
};
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.
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.
To publish the library to npm:
pnpm publish