loginRequired.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const async = require("async");
  2. const cache = require("../../cache");
  3. const utils = require("../../utils");
  4. // const logger = moduleManager.modules["logger"];
  5. console.log(cache);
  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. [
  13. next => {
  14. cache
  15. .runJob("HGET", {
  16. table: "sessions",
  17. key: session.sessionId
  18. })
  19. .then(session => next(null, session))
  20. .catch(next);
  21. },
  22. (session, next) => {
  23. if (!session || !session.userId)
  24. return next("Login required.");
  25. this.session = session;
  26. next();
  27. }
  28. ],
  29. async err => {
  30. if (err) {
  31. err = await utils.runJob("GET_ERROR", { error: err });
  32. console.log(
  33. "LOGIN_REQUIRED",
  34. `User failed to pass login required check.`
  35. );
  36. return cb({ status: "failure", message: err });
  37. }
  38. console.log(
  39. "LOGIN_REQUIRED",
  40. `User "${session.userId}" passed login required check.`,
  41. false
  42. );
  43. next.apply(null, args);
  44. }
  45. );
  46. };
  47. };