loginRequired.js 890 B

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