ready to test

Testing

Signed-off-by: Kenneth Obsequio <k80308392@gmail.com>
This commit is contained in:
2026-06-22 10:06:58 +08:00
parent bf48c95467
commit 439bb33f77
189 changed files with 17559 additions and 686 deletions
+39 -1
View File
@@ -45,4 +45,42 @@ const authenticate = async (req, res, next) => {
}
};
module.exports = { authenticate };
/**
* Soft authentication for endpoints that are useful both authenticated and unauthenticated.
*
* Behaviour:
* - No Authorization header → guest mode (req.user = null, continues)
* - Valid token → authenticated (req.user = user, continues)
* - Present but invalid/expired token → 401 (a token was attempted; reject it)
*
* The distinction between "no token" and "bad token" matters:
* silently ignoring a malformed token would allow it to be used as a scapegoat
* to probe the endpoint without being flagged as unauthenticated.
*/
const softAuthenticate = async (req, res, next) => {
const authHeader = req.headers.authorization;
// No token at all → guest mode
if (!authHeader?.startsWith('Bearer ')) {
req.user = null;
return next();
}
// Token present → validate it fully (same rules as authenticate)
try {
const token = authHeader.split(' ')[1];
const decoded = verifyAccessToken(token);
const user = await mdl_Users.findByPk(decoded.user_id, {
attributes: { exclude: ['password', 'otp_code', 'otp_expires_at'] },
});
req.user = (user && user.is_active) ? user : null;
next();
} catch (err) {
if (err.name === 'TokenExpiredError')
return R.error(res, 'Token expired. Please log in again.', 401);
return R.error(res, 'Invalid token.', 401);
}
};
module.exports = { authenticate, softAuthenticate };