app2-api/middlewares/accessValidator.js
2019-09-12 12:03:07 +02:00

26 lines
824 B
JavaScript

'use strict';
const passport = require('passport');
const httpStatus = require('http-status');
const compose = require('../helpers/middleware.helper');
const isRegisteredUserEmail = passport.authenticate('local-email', { session: false });
const isRegisteredUserPhone = passport.authenticate('local-phone', { session: false });
const isLoggedUser = passport.authenticate('jwt', { session: false });
const isAdministratorUser = compose([isLoggedUser,
(req, res, next) => {
const user = req.user;
if (user.level >= 8) {
next();
} else {
return res.status(httpStatus.UNAUTHORIZED).send('Unauthorized. User is not administrator.');
}
}
]);
module.exports = {
isRegisteredUserEmail,
isRegisteredUserPhone,
isLoggedUser,
isAdministratorUser
};