feat : admin feature
This commit is contained in:
28
src/middlewares/admin.middleware.js
Normal file
28
src/middlewares/admin.middleware.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const { verifyToken } = require('../util/jwt');
|
||||
const { logger } = require('../util/logger');
|
||||
|
||||
function checkAdmin (req,res,next){
|
||||
const authHeader = req.headers.authorization;
|
||||
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return res
|
||||
.status(401)
|
||||
.json({ error: 'missing or malformed authorization header' });
|
||||
}
|
||||
|
||||
const token = authHeader.split(' ')[1];
|
||||
try {
|
||||
const payload = verifyToken(token);
|
||||
// console.log("hi",payload);
|
||||
if(payload.customerNo && payload.role === 'admin'){
|
||||
req.admin = payload.customerNo;
|
||||
next();
|
||||
}
|
||||
else
|
||||
return res.status(403).json({error :'Only admin can access'})
|
||||
} catch (err) {
|
||||
logger.error(err, 'error verifying token');
|
||||
return res.status(401).json({ error: 'invalid or expired token' });
|
||||
}
|
||||
}
|
||||
module.exports = checkAdmin;
|
||||
Reference in New Issue
Block a user