mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -33,9 +33,21 @@ 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) return R.error(res, 'User not found.', 401);
|
||||
if (!user.is_active) return R.error(res, 'Account is deactivated.', 403);
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
next();
|
||||
} catch (err) {
|
||||
@@ -74,7 +86,21 @@ const softAuthenticate = async (req, res, next) => {
|
||||
attributes: { exclude: ['password', 'otp_code', 'otp_expires_at'] },
|
||||
});
|
||||
|
||||
req.user = (user && user.is_active) ? user : null;
|
||||
if (!user || !user.is_active) {
|
||||
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 });
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
next();
|
||||
} catch (err) {
|
||||
if (err.name === 'TokenExpiredError')
|
||||
|
||||
Reference in New Issue
Block a user