one-time-jwt

One-Time JWT SDK

/_next/static/media/otj.0kdvbp9n8jm3j.png

One-Time JWT is a TypeScript library for creating and verifying one-time JSON Web Tokens (JWTs) that come with a one-time password (OTP). Each token is tied to a purpose, such as login, and can only be verified with its matching OTP.

What it does

  • Creates a token and its OTP for a given purpose and payload.
  • Verifies a token with its OTP and returns the payload.
  • Generates OTPs as digits, letters, alphanumeric characters, or base64, with a chosen length.
  • Can limit how many tokens are allowed per purpose.
  • Offers "safe" versions of create and verify that return a [result, error] pair instead of throwing, plus specific error classes such as IncorrectOTPError.

Usage

npm install one-time-jwt
const jwt = new OneTimeJwt('baseSecret')
const result = await jwt.createToken('login', { userId: 123 }, { otpType: 'digits', otpLength: 6 })
console.log(result.token, result.otp)

const payload = await jwt.verifyToken('login', result.token, result.otp)
console.log(payload)