loginRequired.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 =>
  6. function loginRequired(session, ...args) {
  7. const cb = args[args.length - 1];
  8. async.waterfall(
  9. [
  10. next => {
  11. CacheModule.runJob(
  12. "HGET",
  13. {
  14. table: "sessions",
  15. key: session.sessionId
  16. },
  17. this
  18. )
  19. .then(session => next(null, session))
  20. .catch(next);
  21. },
  22. (session, next) => {
  23. if (!session || !session.userId) return next("Login required.");
  24. return next();
  25. }
  26. ],
  27. async err => {
  28. if (err) {
  29. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  30. this.log("LOGIN_REQUIRED", `User failed to pass login required check.`);
  31. return cb({ status: "error", message: err });
  32. }
  33. this.log("LOGIN_REQUIRED", `User "${session.userId}" passed login required check.`);
  34. return destination.apply(this, [session].concat(args));
  35. }
  36. );
  37. };