loginRequired.js 971 B

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