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

33 lines
866 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) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "Vous n'êtes pas connecté.",
statusCode: 403
})
2020-08-03 12:04:07 +02:00
}
Users.findOne({ where: { id: req.userId } })
2020-08-03 14:14:45 +02:00
.then(user => {
2020-08-03 12:04:07 +02:00
if (!user) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "Le mot de passe ou l'adresse email n'est pas valide.",
statusCode: 403
})
2020-08-03 12:04:07 +02:00
}
if (!user.isAdmin) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "Vous n'êtes pas administrateur.",
statusCode: 403
})
2020-08-03 12:04:07 +02:00
}
next()
})
2020-08-03 14:14:45 +02:00
.catch(error => {
2020-08-03 12:04:07 +02:00
console.log(error)
return errorHandling(next, serverError)
})
}