Fix jwt-token
This commit is contained in:
@@ -104,22 +104,51 @@ async function authenticateToken(req, res, next) {
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
// Only log unexpected errors, not common JWT validation failures
|
||||
// Log authentication errors for monitoring (but not in tests)
|
||||
if (process.env.NODE_ENV !== 'test' || error.name === 'TypeError') {
|
||||
console.error('Token verification error:', error);
|
||||
}
|
||||
|
||||
// Handle specific JWT errors
|
||||
if (error.name === 'TokenExpiredError') {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: 'Token expired'
|
||||
console.error('🔐 Authentication error:', {
|
||||
error: error.name,
|
||||
message: error.message,
|
||||
userAgent: req.headers['user-agent'],
|
||||
ip: req.ip || req.connection.remoteAddress,
|
||||
path: req.path
|
||||
});
|
||||
}
|
||||
|
||||
// Handle specific JWT errors with detailed responses
|
||||
if (error.name === 'TokenExpiredError') {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'TOKEN_EXPIRED',
|
||||
message: 'Token expired',
|
||||
redirectToLogin: true
|
||||
});
|
||||
}
|
||||
|
||||
if (error.name === 'JsonWebTokenError') {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'INVALID_TOKEN',
|
||||
message: 'Invalid token',
|
||||
redirectToLogin: true
|
||||
});
|
||||
}
|
||||
|
||||
if (error.name === 'NotBeforeError') {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'TOKEN_NOT_ACTIVE',
|
||||
message: 'Token not active',
|
||||
redirectToLogin: true
|
||||
});
|
||||
}
|
||||
|
||||
// Generic authentication error
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
message: 'Invalid token'
|
||||
error: 'AUTHENTICATION_FAILED',
|
||||
message: 'Authentication failed',
|
||||
redirectToLogin: true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user