@jaiveer2004/otp-generator
v1.0.2
Published
Used to generate random OTP's and used to verify it.
Maintainers
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-generatorUsage
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 falseCommonJS
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 falseAPI 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:trueif the OTP matches,falseotherwise
Example:
const otp = otpGenerator(6);
const isValid = otpVerify(otp); // Returns true
const isInvalid = otpVerify('wrong'); // Returns falseComplete 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
