generateotp-ts
A TypeScript library for generating and verifying One-Time Passwords (OTPs) using JSON Web Tokens (JWT).
Features
- Generate n-digit OTPs
- Set custom expiration time for OTPs
- Verify OTPs with tokens
- Encode and decode OTPs using JWT
Installation
Install generateotp-ts
using npm:
npm install generateotp-ts
Usage
Import and use the generateOtp
and verifyOtp
functions in your TypeScript or JavaScript project:
import { generateOtp, verifyOtp } from "generateotp-ts";
const { otp, token } = generateOtp(6, "5m");
console.log("Generated OTP:", otp);
console.log("Token:", token);
const isValidOtp = verifyOtp(otp, token);
console.log("Is OTP valid?", isValidOtp);
generateOtp()
The generateOtp()
function generates an n-digit OTP and returns an object containing the generated OTP and its corresponding JWT-encoded token.
Function signature:
function generateOtp(
digits: number,
expiration: string | number | undefined = undefined,
secret: string = ""
): { otp: number; token: string };
verifyOtp()
The verifyOtp()
function verifies the provided OTP against the JWT-encoded token and returns true
if it's valid, or false
if it's invalid or expired.
Function signature:
function verifyOtp(otp: number, token: string, secret: string = ""): boolean;