diff --git a/backend/src/middleware/auth.js b/backend/src/middleware/auth.js new file mode 100644 index 0000000..1bc3912 --- /dev/null +++ b/backend/src/middleware/auth.js @@ -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 };