add: more things

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-07-04 03:35:09 +08:00
parent a572c1e25f
commit 3c13bb9821
36 changed files with 896 additions and 928 deletions
+14 -5
View File
@@ -25,6 +25,7 @@
* GET /api/auth/google/callback
***********************************************************************************************************************************************************************/
const bcrypt = require('bcryptjs');
const crypto = require('crypto');
const sequelize = require('../config/db.config')
const mdl_Users = require('../models/users/users.mdl');
const mdl_UserSessions = require('../models/users/user_sessions.mdl');
@@ -103,10 +104,13 @@ exports.register = async (req, res) => {
}, { transaction });
}
await sendEmail({ to: email, type: 'OTP', data: { otp } });
await transaction.commit();
// Fire-and-forget: don't let an SMTP hiccup or template issue roll back
// an otherwise-successful registration — resendOTP covers redelivery.
sendEmail({ to: email, type: 'OTP', data: { otp } })
.catch(err => console.error('[AUTH] Failed to send OTP email:', err));
// Fire-and-forget: notify admins — explicit group or NOGRP fallback
if (group) {
renderNotification({ type: 'user_registration', data: {
@@ -146,7 +150,9 @@ exports.verifyOTP = async (req, res) => {
if (!user) return R.error(res, 'User not found.', 404);
if (user.is_verified) return R.error(res, 'Account already verified.', 400);
if (!crypto.timingSafeEqual(Buffer.from(user.otp_code ?? ''), Buffer.from(otp)))
const storedOTP = Buffer.from(user.otp_code ?? '');
const givenOTP = Buffer.from(otp ?? '');
if (storedOTP.length !== givenOTP.length || !crypto.timingSafeEqual(storedOTP, givenOTP))
return R.error(res, 'Invalid OTP.', 400);
if (isOTPExpired(user.otp_expires_at)) return R.error(res, 'OTP has expired. Please request a new one.', 400);
@@ -235,10 +241,13 @@ exports.resendOTP = async (req, res) => {
const otp = generateOTP();
await user.update({ otp_code: otp, otp_expires_at: getOTPExpiry() }, { transaction });
await sendEmail({ to: email, type: "OTP", data: { otp } });
await transaction.commit();
// Fire-and-forget: don't let an SMTP hiccup or template issue roll back
// the already-persisted OTP refresh.
sendEmail({ to: email, type: "OTP", data: { otp } })
.catch(err => console.error('[AUTH] Failed to send OTP email:', err));
return R.success(res, 'A new OTP has been sent to your email.');
} catch (err) {
await transaction.rollback();