app2-api/middlewares/accessValidator.js
David 0e4489c72f #22 -> Poder acceder a la aplicación sin registro.
/events
/events/cities
/events/next
/events/past
/events/yesterday
/events/today
/events/tomorrow
/events/current
/events/featured
/events/:id
/events/:id/comments
/events/:id/multimedias
/locations
/locations/:id
/speakers
/speakers/featured
/speakers/last
/speakers/:id
/speakers/:id/similar
2022-02-08 17:33:23 +01:00

36 lines
1.1 KiB
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 isOptionalUser = (req, res, next) => {
const token = ((req && req.headers && req.headers['x-access-token']) ? req.headers['x-access-token'] : null);
if (token) {
isLoggedUser(req, res, next)
} else {
next();
}
}
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,
isOptionalUser,
isAdministratorUser
};