loginRequired.js 1.6 KB

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