UNPKG

universal_authentication

Version:

Seamless and Secure Authentication for Modern Web Applications: Easily integrate OTP-based email verification, Google OAuth, GitHub, Microsoft, and Okta login into your Node.js app. Modular, flexible, and database-agnostic, this package simplifies user au

38 lines (31 loc) 985 B
import { otpRecord } from "../../config/otpRecord.config"; export const otpStore: Map<string, otpRecord> = new Map(); // Function to generate an OTP and store it in the otpStore export const generateOtp = () => { return Math.floor(100000 + Math.random() * 900000).toString(); }; export const storeOtp = (email: string, otp: string): void => { const expiresAt = Date.now() + 5 * 60 * 1000; otpStore.set(email, { otp, expiresAt }); }; //verify otp from memory export const verifyOtp = (email: string, otp: string): boolean => { const record = otpStore.get(email); if (!record) { return false; } if (record.expiresAt < Date.now()) { otpStore.delete(email); return false; } return record.otp === otp; }; //clean up the expired otp export const cleanUpOtp = (): void => { const now = Date.now(); otpStore.forEach((record, email) => { if (record.expiresAt < now) { otpStore.delete(email); } }); };