add: more things

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-07-06 15:21:55 +08:00
parent 7ba03ad4db
commit 095a0d4b3c
15 changed files with 756 additions and 211 deletions
+14 -20
View File
@@ -15,6 +15,7 @@
const { verifyAccessToken } = require('../utils/token.util');
const mdl_Users = require('../models/users/users.mdl');
const R = require('../utils/response.util');
const { checkAccountStatus } = require('../services/accountStatus.service');
/**
* Validates JWT and loads user from DB.
@@ -33,19 +34,15 @@ const authenticate = async (req, res, next) => {
attributes: { exclude: ['password', 'otp_code', 'otp_expires_at'] },
});
if (!user) return R.error(res, 'User not found.', 401);
if (!user.is_active) return R.error(res, 'Account is deactivated.', 403);
if (!user) return R.error(res, 'User not found.', 401);
if (user.is_banned) {
const stillBanned = !user.ban_expires_at || new Date() < new Date(user.ban_expires_at);
if (stillBanned) {
return R.error(res, 'Your account has been suspended.', 403, {
banned: true,
ban_expires_at: user.ban_expires_at ?? null,
});
}
// Expired temporary ban — auto-lift so the user can log in again
await user.update({ is_banned: false, ban_expires_at: null });
const status = await checkAccountStatus(user);
if (!status.ok) {
if (status.code === 'deactivated') return R.error(res, 'Account is deactivated.', 403);
return R.error(res, 'Your account has been suspended.', 403, {
banned: true,
ban_expires_at: status.ban_expires_at,
});
}
req.user = user;
@@ -86,18 +83,15 @@ const softAuthenticate = async (req, res, next) => {
attributes: { exclude: ['password', 'otp_code', 'otp_expires_at'] },
});
if (!user || !user.is_active) {
if (!user) {
req.user = null;
return next();
}
if (user.is_banned) {
const stillBanned = !user.ban_expires_at || new Date() < new Date(user.ban_expires_at);
if (stillBanned) {
req.user = null;
return next();
}
await user.update({ is_banned: false, ban_expires_at: null });
const status = await checkAccountStatus(user);
if (!status.ok) {
req.user = null;
return next();
}
req.user = user;