This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/middlewares/isAdmin.js

23 lines
896 B
JavaScript

const errorHandling = require('../assets/utils/errorHandling');
const { serverError } = require('../assets/config/errors');
const Users = require('../models/users');
module.exports = (req, _res, next) => {
if (!req.userId) {
return errorHandling(next, { message: "Vous n'êtes pas connecté.", statusCode: 403 });
}
Users.findOne({ where: { id: req.userId } })
.then((user) => {
if (!user) {
return errorHandling(next, { message: "Le mot de passe ou l'adresse email n'est pas valide.", statusCode: 403 });
}
if (!user.isAdmin) {
return errorHandling(next, { message: "Vous n'êtes pas administrateur.", statusCode: 403 });
}
next();
})
.catch((error) => {
console.log(error);
errorHandling(next, serverError);
});
}