otp-generator-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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";

// Generate a 6-digit OTP with a 5-minute expiration time
const { otp, token } = generateOtp(6, "5m");

console.log("Generated OTP:", otp);
console.log("Token:", token);

// Verify the OTP with the 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;

Package Sidebar

Install

npm i otp-generator-ts

Weekly Downloads

5

Version

1.0.0

License

ISC

Unpacked Size

5.63 kB

Total Files

6

Last publish

Collaborators

  • jst_htet