prsm/packages/jwt
2024-08-27 18:16:34 -04:00
..
src relocate these 2024-08-27 18:16:34 -04:00
.npmignore relocate these 2024-08-27 18:16:34 -04:00
bump.config.ts relocate these 2024-08-27 18:16:34 -04:00
bun.lockb relocate these 2024-08-27 18:16:34 -04:00
package.json relocate these 2024-08-27 18:16:34 -04:00
README.md relocate these 2024-08-27 18:16:34 -04:00
tsconfig.json relocate these 2024-08-27 18:16:34 -04:00
tsup.config.ts relocate these 2024-08-27 18:16:34 -04:00

jwt

A package for encoding, decoding, and verifying JWTs.

Installation

npm install @prsm/jwt

Encoding

import { encode } from "@prsm/jwt";

const payload = {
  iat: Date.now(),
  exp: Date.now() + 3600,
};

const token = encode(payload, process.env.SIGNING_KEY);

Verifying

import { verify } from "@prsm/jwt";

const result = verify(token, process.env.SIGNING_KEY);

if (!result.sig) throw new Error("signature verification failed");
if (result.exp) throw new Error("token has expired");
if (!result.nbf) throw new Error("token is not yet valid")

// token payload is available at result.decoded.payload

Decoding

import { decode } from "@prsm/jwt";

const result = decode(token);
// { header: { alg: "HS256", typ: "JWT" }, payload: { iat: 123456789, exp: 123456789 }, signature: "..."