🔐 tiny JWT middleware for Node.js
Go to file
v1rtl e1e5814601 fix: make `next` required to avoid missing coverage 2023-10-27 21:06:43 +03:00
.github feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
.husky feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
src fix: make `next` required to avoid missing coverage 2023-10-27 21:06:43 +03:00
tests feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
.eslintrc feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
.gitignore feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
.prettierrc add missing configs 2021-07-04 21:45:13 +03:00
README.md feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
bun.lockb feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
commitlint.config.cjs feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
package.json feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
tsconfig.build.json feat: move to bun + tsc 2023-10-27 21:04:17 +03:00
tsconfig.json feat: move to bun + tsc 2023-10-27 21:04:17 +03:00

README.md

@tinyhttp/jwt

NPM NPM GitHub Workflow Status Coverage

JWT middleware for HTTP servers.

Install

bun i @tinyhttp/jwt

API

import { jwt } from '@tinyhttp/jwt'

Options

jwt(options)

  • secret: can be an array of strings (in case you are using private / public key encryption), or just a string if you are using basic HMAC signing (see the examples below)
  • algorithm? ("HS256"): the algorithm used to sign and verify the token
  • audience?: the expected "audience" of the jwt token
  • issuer?: who issued this token
  • expiresIn?: expiration time of the token (ex: 1d for 1 day)
  • notBefore?: not before date of the token (ex: 20m for 20 minutes)
  • requestHeaderName? ("Authorization"): the name of the header contaning the Bearer token
  • responseHeaderName? ("X-Token"): the name of the response header containing the new signed token that will be used later on
  • getToken(string)?: string: the method used for ex

Example

Basic secret

import { App } from '@tinyhttp/app'
import { jwt } from '@tinyhttp/jwt'

new App()
  .use(jwt({ secret: 'secret', algorithm: 'HS256' }))
  .get('/', (req, res) => res.send(`Data inside the payload: ${req['user']}`))
  .listen(8080)

Private / Public key

import { App } from '@tinyhttp/app'
import { jwt } from '@tinyhttp/jwt'

new App()
  .use(jwt({ secret: ['PRIVATE KEY', 'PUBLIC KEY'], algorithm: 'RS256' }))
  .get('/', (req, res) => res.send(`Data inside the payload: ${req['user']}`))
  .listen(8080)