Spaces:
Sleeping
Sleeping
File size: 930 Bytes
6931883 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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,
};
|