Files
yourpart3/backend/middleware/authMiddleware.js
Torsten Schulz (local) 1ab9407e79
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
2026-07-21 09:08:15 +02:00

26 lines
892 B
JavaScript
Executable File

import User from '../models/community/user.js';
import { updateUserTimestamp } from '../utils/redis.js';
export const authenticate = async (req, res, next) => {
const { userid: userId, authcode: authCode } = req.headers;
if (!userId || !authCode) {
return res.status(401).json({ error: 'Unauthorized: Missing credentials' });
}
const user = await User.findOne({ where: { hashedId: userId, authCode } });
if (!user) {
return res.status(401).json({ error: 'Unauthorized: Invalid credentials' });
}
if (!user.active) {
return res.status(403).json({ error: 'Unauthorized: User blocked' });
}
try {
await updateUserTimestamp(user.id);
} catch (error) {
console.error('Fehler beim Aktualisieren des Zeitstempels:', error);
}
next();
};
// Default export für Kompatibilität
export default { authenticate };