loginRequired.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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) => {
  19. next(null, session);
  20. })
  21. .catch(next);
  22. },
  23. (session, next) => {
  24. if (!session || !session.userId)
  25. return next("Login required.");
  26. this.session = session;
  27. next();
  28. },
  29. ],
  30. async (err) => {
  31. if (err) {
  32. err = await utils.runJob("GET_ERROR", { error: err });
  33. console.log(
  34. "LOGIN_REQUIRED",
  35. `User failed to pass login required check.`
  36. );
  37. return cb({ status: "failure", message: err });
  38. }
  39. console.log(
  40. "LOGIN_REQUIRED",
  41. `User "${session.userId}" passed login required check.`,
  42. false
  43. );
  44. next.apply(null, args);
  45. }
  46. );
  47. };
  48. };