loginRequired.js 914 B

12345678910111213141516171819202122232425262728293031
  1. const cache = require('../../cache');
  2. const utils = require('../../utils');
  3. const logger = require('../../logger');
  4. const async = require('async');
  5. module.exports = function(next) {
  6. return function(session) {
  7. let args = [];
  8. for (let prop in arguments) args.push(arguments[prop]);
  9. let cb = args[args.length - 1];
  10. async.waterfall([
  11. (next) => {
  12. cache.hget('sessions', session.sessionId, next);
  13. },
  14. (session, next) => {
  15. if (!session || !session.userId) return next('Login required.');
  16. this.session = session;
  17. next();
  18. }
  19. ], (err) => {
  20. if (err) {
  21. err = utils.getError(err);
  22. logger.info("LOGIN_REQUIRED", `User failed to pass login required check.`);
  23. return cb({status: 'failure', message: err});
  24. }
  25. logger.info("LOGIN_REQUIRED", `User "${session.userId}" passed login required check.`, false);
  26. args.push(session.userId);
  27. next.apply(null, args);
  28. });
  29. }
  30. };