loginRequired.js 955 B

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