feat: added authorization for one api

This commit is contained in:
Sathishkumar Krishnan
2021-12-24 00:58:02 +05:30
parent 26c4c54114
commit 878ec6d0e0
6 changed files with 86 additions and 27 deletions

View File

@@ -0,0 +1,16 @@
const UserRole = require("../../models/UserRole");
const { AuthorizeUser } = require("../../config/auth");
const { SUPER_ADMIN_ROLE, AUTHORIZATION_FAILURE_ERROR_MESSAGE } = require("../../config/constants");
module.exports = {
SuperAdminCheck: async (req, res, next) => {
const SuperAdmin = await UserRole.findOne({ name: SUPER_ADMIN_ROLE });
if (AuthorizeUser(req.locals.user, [SuperAdmin.id])) {
next();
} else {
res
.status(403)
.send({ success: false, error: AUTHORIZATION_FAILURE_ERROR_MESSAGE });
}
},
};