Modern TOTP for JavaScript
A comprehensive TOTP (Time-based One-Time Password) library for JavaScript applications. Secure, lightweight, and developer-friendly 2FA implementation with full TypeScript support.
npm install modern-totpEverything you need for 2FA
Built for modern JavaScript/TypeScript applications with security, performance, and developer experience in mind.
Secure by Default
RFC 6238 compliant implementation with cryptographically secure random secret generation.
Zero Dependencies
Lightweight library with no external dependencies. Perfect for modern JavaScript applications.
Universal Compatibility
Works with all major authenticator apps including Google Authenticator, Authy, and 1Password.
TypeScript Ready
Full TypeScript support with comprehensive type definitions and IntelliSense.
Simple API
Clean, intuitive API designed for developer productivity and ease of integration.
Battle Tested
Extensively tested with comprehensive test coverage for production reliability.
Simple to integrate
Get started with just a few lines of code. Clean API designed for developer productivity.
Basic Usage
import { generateSecret, generateTOTP, verifyTOTP } from 'modern-totp';
// Generate a secret for the user
const secret = generateSecret();
// Generate a TOTP code
const token = generateTOTP(secret);
// Verify a TOTP code
const isValid = verifyTOTP(token, secret);
console.log(isValid); // trueQR Code Generation
import { generateTOTP, generateQRCodeURL } from 'modern-totp';
const secret = 'JBSWY3DPEHPK3PXP';
const issuer = 'MyApp';
const accountName = 'user@example.com';
// Generate QR code URL for authenticator apps
const qrCodeURL = generateQRCodeURL(secret, issuer, accountName);
// Display QR code to user for scanning
console.log(qrCodeURL);