loginRequired.js 933 B

123456789101112131415161718192021222324252627282930313233343536
  1. import async from "async";
  2. import cache from "../../cache";
  3. import utils from "../../utils";
  4. // const logger = moduleManager.modules["logger"];
  5. export default destination => (session, ...args) => {
  6. const cb = args[args.length - 1];
  7. async.waterfall(
  8. [
  9. next => {
  10. cache
  11. .runJob("HGET", {
  12. table: "sessions",
  13. key: session.sessionId
  14. })
  15. .then(session => next(null, session))
  16. .catch(next);
  17. },
  18. (session, next) => {
  19. if (!session || !session.userId) return next("Login required.");
  20. return next();
  21. }
  22. ],
  23. async err => {
  24. if (err) {
  25. err = await utils.runJob("GET_ERROR", { error: err });
  26. console.log("LOGIN_REQUIRED", `User failed to pass login required check.`);
  27. return cb({ status: "failure", message: err });
  28. }
  29. console.log("LOGIN_REQUIRED", `User "${session.userId}" passed login required check.`, false);
  30. return destination(session, ...args);
  31. }
  32. );
  33. };