2019-04-24 21:01:54 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-04-25 09:25:33 +00:00
|
|
|
const passport = require('passport');
|
2019-05-09 16:23:54 +00:00
|
|
|
const httpStatus = require('http-status');
|
|
|
|
|
const compose = require('../helpers/middleware.helper');
|
2019-04-24 21:01:54 +00:00
|
|
|
|
2019-06-21 08:40:28 +00:00
|
|
|
const isRegisteredUserEmail = passport.authenticate('local-email', { session: false });
|
|
|
|
|
const isRegisteredUserPhone = passport.authenticate('local-phone', { session: false });
|
2019-05-09 16:23:54 +00:00
|
|
|
const isLoggedUser = passport.authenticate('jwt', { session: false });
|
2022-12-07 12:45:54 +00:00
|
|
|
const isAPIKeyUser = passport.authenticate('api-key', { session: false });
|
|
|
|
|
|
2022-02-08 16:33:23 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 16:23:54 +00:00
|
|
|
const isAdministratorUser = compose([isLoggedUser,
|
|
|
|
|
(req, res, next) => {
|
|
|
|
|
const user = req.user;
|
2019-08-05 15:34:59 +00:00
|
|
|
if (user.level >= 8) {
|
2019-05-09 16:23:54 +00:00
|
|
|
next();
|
|
|
|
|
} else {
|
2019-08-17 19:41:22 +00:00
|
|
|
return res.status(httpStatus.UNAUTHORIZED).send('Unauthorized. User is not administrator.');
|
2019-05-09 16:23:54 +00:00
|
|
|
}
|
2019-04-24 21:01:54 +00:00
|
|
|
}
|
2019-05-09 16:23:54 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2019-06-21 08:40:28 +00:00
|
|
|
isRegisteredUserEmail,
|
2022-12-07 12:45:54 +00:00
|
|
|
isRegisteredUserPhone,
|
2019-05-09 16:23:54 +00:00
|
|
|
isLoggedUser,
|
2022-02-08 16:33:23 +00:00
|
|
|
isOptionalUser,
|
2022-12-07 12:45:54 +00:00
|
|
|
isAdministratorUser,
|
|
|
|
|
isAPIKeyUser
|
2019-04-24 21:01:54 +00:00
|
|
|
};
|