loginRequired.js 1.1 KB

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