npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jaiveer2004/otp-generator

v1.0.2

Published

Used to generate random OTP's and used to verify it.

Readme

@jaiveer2004/otp-generator

A simple and lightweight npm package for generating random OTPs (One-Time Passwords) and verifying them.

Features

  • 🔢 Generate random numeric OTPs of any length
  • ✅ Simple OTP verification functionality
  • 🚀 Easy to use and integrate
  • 📦 Lightweight with no dependencies
  • 🔒 Secure random number generation

Installation

npm install @jaiveer2004/otp-generator

Usage

ES6 Modules (Recommended)

import { otpGenerator, otpVerify } from '@jaiveer2004/otp-generator';

// Generate a 6-digit OTP
const otp = otpGenerator(6);
console.log('Generated OTP:', otp); // e.g., "123456"

// Verify the OTP
const isValid = otpVerify('123456');
console.log('Is valid:', isValid); // true or false

CommonJS

const { otpGenerator, otpVerify } = require('@jaiveer2004/otp-generator');

// Generate a 4-digit OTP
const otp = otpGenerator(4);
console.log('Generated OTP:', otp); // e.g., "7842"

// Verify the OTP
const isValid = otpVerify('7842');
console.log('Is valid:', isValid); // true or false

API Reference

otpGenerator(length)

Generates a random numeric OTP of the specified length.

Parameters:

  • length (number): The desired length of the OTP

Returns:

  • string: A random numeric OTP

Example:

const otp = otpGenerator(6); // Returns a 6-digit OTP like "485920"

otpVerify(userOTP)

Verifies if the provided OTP matches the last generated OTP.

Parameters:

  • userOTP (string): The OTP to verify

Returns:

  • boolean: true if the OTP matches, false otherwise

Example:

const otp = otpGenerator(6);
const isValid = otpVerify(otp); // Returns true
const isInvalid = otpVerify('wrong'); // Returns false

Complete Example

import { otpGenerator, otpVerify } from '@jaiveer2004/otp-generator';

// Simulate OTP generation and verification flow
function authenticateUser() {
  // Step 1: Generate OTP
  const otp = otpGenerator(6);
  console.log('OTP sent to user:', otp);
  
  // Step 2: Simulate user input (in real app, this would come from user)
  const userInput = '123456'; // This would be input from user
  
  // Step 3: Verify OTP
  const isAuthenticated = otpVerify(userInput);
  
  if (isAuthenticated) {
    console.log('✅ Authentication successful!');
  } else {
    console.log('❌ Invalid OTP. Please try again.');
  }
}

authenticateUser();

Use Cases

  • Two-Factor Authentication (2FA): Add an extra layer of security to user accounts
  • Password Reset: Verify user identity before allowing password changes
  • Account Verification: Confirm email addresses or phone numbers
  • Transaction Verification: Secure financial or sensitive operations
  • Login Verification: Additional security for login processes

Important Notes

⚠️ Security Considerations:

  • This package stores the OTP in memory. In production applications, consider storing OTPs securely (e.g., in a database with expiration)
  • Implement OTP expiration logic in your application
  • Use HTTPS when transmitting OTPs
  • Consider rate limiting to prevent brute force attacks

⚠️ State Management:

  • The package maintains the last generated OTP in memory
  • Each call to otpGenerator() overwrites the previous OTP
  • For multi-user applications, implement your own OTP storage mechanism

Keywords

otp, otp-generator, otp-handler, otps, passcode, one-time-password, authentication, 2fa, verification

License

ISC

Author

Jaiveer Singh

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues

If you encounter any issues or have suggestions, please create an issue on the GitHub repository.


Made with ❤️ by Jaiveer Singh