@ooneex/jwt is a small toolkit for working with JSON Web Tokens. It wraps the JOSE library to sign tokens with the HS256 algorithm, validate them, and read their header and payload. The signing secret is read from the JWT_SECRET environment variable through @ooneex/app-env.
Installation
Add the package to your project with Bun.Usage
TheJwt class takes an AppEnv instance that exposes JWT_SECRET. Once constructed, you can create tokens, check their validity, and decode them.
Verifying a token
isValid checks both the signature and the standard claims (such as expiration), returning a boolean instead of throwing.
Reading the payload and header
Decoding does not verify the signature, so only call these afterisValid on tokens you trust.
When to use it
- Issuing stateless access tokens for an API or web session after a user logs in.
- Verifying incoming bearer tokens in middleware before granting access to a route.
- Reading claims (user id, role, audience, expiry) from a token you have already verified.
- You do not need it for opaque session tokens stored in a database, or when an external identity provider already issues and validates the tokens for you.