mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user