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

24 lines
798 B
JavaScript
Raw Normal View History

2020-08-03 12:04:07 +02:00
const errorHandling = require('../assets/utils/errorHandling')
const { serverError } = require('../assets/config/errors')
const Users = require('../models/users')
2020-03-25 16:23:43 +01:00
module.exports = (req, _res, next) => {
2020-08-03 12:04:07 +02:00
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)
return errorHandling(next, serverError)
})
}