26 lines
689 B
JavaScript
26 lines
689 B
JavaScript
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 };
|