chnages
chnages
This commit is contained in:
1 parent
3e22355510
commit
e22a8dc248
71 files changed
+24775
-1
No files matched your search
@@ -0,0 +1,30 @@
|
||||
// ============================================================
|
||||
// otp.js — One-Time Password (OTP) generator
|
||||
// ============================================================
|
||||
|
||||
const crypto = require('crypto');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
// Generate a random 6-digit OTP using crypto.randomInt()
|
||||
function generateOTP() {
|
||||
return crypto.randomInt(100000, 1000000).toString();
|
||||
}
|
||||
|
||||
// Get the expiry time: 5 minutes from now
|
||||
function getOTPExpiry() {
|
||||
const expiry = new Date();
|
||||
expiry.setMinutes(expiry.getMinutes() + 5);
|
||||
return expiry;
|
||||
}
|
||||
|
||||
// Hash an OTP with bcrypt before storing in DB
|
||||
function hashOTP(otp) {
|
||||
return bcrypt.hashSync(String(otp), 10);
|
||||
}
|
||||
|
||||
// Verify an OTP against a stored bcrypt hash
|
||||
function verifyOTPHash(inputOTP, storedHash) {
|
||||
return bcrypt.compareSync(String(inputOTP), storedHash);
|
||||
}
|
||||
|
||||
module.exports = { generateOTP, getOTPExpiry, hashOTP, verifyOTPHash };
|
||||
Reference in new issue
Block a user