39 lines
1017 B
JavaScript
39 lines
1017 B
JavaScript
/**
|
|
* Copyright (c) 2026 Niolla
|
|
* All rights reserved.
|
|
*
|
|
* This source code is proprietary and confidential.
|
|
* Unauthorized copying, modification, distribution, or use
|
|
* of this file, via any medium, is strictly prohibited.
|
|
*/
|
|
|
|
// app/utils/otp.util.js
|
|
|
|
const otpCache = new Map();
|
|
|
|
function generateOTP(key){
|
|
const otp = Math.floor(100000 + Math.random() * 900000).toString();
|
|
saveOTP(key, otp);
|
|
return otp;
|
|
}
|
|
|
|
function saveOTP(key, otp) {
|
|
ttl = parseInt(process.env.LOGIN_OTP_TTL_SECONDS || 300);
|
|
const expiresAt = Date.now() + ttl * 1000; // Convert seconds to milliseconds
|
|
otpCache.set(key, { otp, expiresAt });
|
|
}
|
|
|
|
// Validate OTP
|
|
function validateOTP(key, otp) {
|
|
const record = otpCache.get(key);
|
|
if (!record) return false;
|
|
if (Date.now() > record.expiresAt) {
|
|
otpCache.delete(key);
|
|
return false;
|
|
}
|
|
const isValid = record.otp === otp;
|
|
if (isValid) otpCache.delete(key); // OTP can be used only once
|
|
return isValid;
|
|
}
|
|
|
|
module.exports = { generateOTP, validateOTP }; |