const crypto = require('crypto'); const env = require('../config/env'); const otpStore = new Map(); function generateOtp() { const minimum = 10 ** (env.otpLength - 1); const maximum = 10 ** env.otpLength; return String(minimum + crypto.randomInt(0, maximum - minimum)); } function createOtp(targetKey) { const otpCode = generateOtp(); const expiresAt = Date.now() + env.otpTtlMinutes * 60 * 1000; otpStore.set(targetKey, { otpCode, expiresAt, }); return { otpCode, expiresAt, }; } function verifyOtp(targetKey, otpCode) { const record = otpStore.get(targetKey); if (!record) { return false; } if (record.expiresAt < Date.now()) { otpStore.delete(targetKey); return false; } const isValid = record.otpCode === String(otpCode || '').trim(); if (isValid) { otpStore.delete(targetKey); } return isValid; } module.exports = { createOtp, verifyOtp, };