prsm/packages/jwt
2025-03-27 20:01:21 -04:00
..
src relocate these 2024-08-27 18:16:34 -04:00
.gitignore Add missing .gitignore 2025-03-27 20:01:21 -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 README 2025-03-26 21:56:12 -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

@prsm/jwt

NPM version

A lightweight JWT implementation for encoding, decoding, and verifying JSON Web Tokens.

Installation

npm install @prsm/jwt

Usage

Encoding Tokens

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

// Create a token with standard claims
const token = encode(
  {
    sub: "user123",
    iat: Date.now(),
    exp: Date.now() + 3600000, // 1 hour from now
  },
  "your-secret-key"
);

// Specify algorithm (default is HS256)
const rsaToken = encode(payload, privateKey, "RS256");

Verifying Tokens

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

const result = verify(token, "your-secret-key");

// Check verification results
if (!result.sig) {
  console.error("Invalid signature");
}

if (result.exp) {
  console.error("Token expired");
}

// Access the decoded payload
const { sub, iat } = result.decoded.payload;

Decoding Without Verification

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

const { header, payload, signature } = decode(token);

Supported Algorithms

Algorithm Description
HS256 HMAC with SHA-256 (default)
HS384 HMAC with SHA-384
HS512 HMAC with SHA-512
RS256 RSA Signature with SHA-256
RS384 RSA Signature with SHA-384
RS512 RSA Signature with SHA-512
ES256 ECDSA Signature with SHA-256