Add auth middleware

This commit is contained in:
2025-07-03 16:31:29 +02:00
parent 1802ddaebd
commit 6c84f701ca
+25
View File
@@ -0,0 +1,25 @@
const jwt = require('jsonwebtoken');
const { db } = require('../database/db');
async function adminAuth(req, res, next) {
try {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'No token provided' });
}
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const admin = await db('admin_users').where({ id: decoded.id, is_active: true }).first();
if (!admin) {
return res.status(401).json({ error: 'Invalid token' });
}
req.admin = admin;
next();
} catch (error) {
res.status(401).json({ error: 'Invalid token' });
}
}
module.exports = { adminAuth };