impl JsonWebToken for SvelteKit.
npm install sveltekit-jwtimpl JsonWebToken for SvelteKit.
``sh`
pnpm i -D sveltekit-jwt
- Support Asymmetric and Symmetric signature.
- Auto cache remote JWKs (jku).
`ts
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.
`ts
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:
`bash`
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:
`bash`
pnpm package
To create a production version of the showcase app:
`bash`
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:
`bash``
pnpm publish