auth.js 994 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. let callbacks = [];
  2. const bannedCallbacks = [];
  3. export default {
  4. ready: false,
  5. authenticated: false,
  6. username: "",
  7. userId: "",
  8. role: "default",
  9. banned: null,
  10. ban: {},
  11. getStatus(cb) {
  12. if (this.ready)
  13. cb(this.authenticated, this.role, this.username, this.userId);
  14. else callbacks.push(cb);
  15. },
  16. setBanned(ban) {
  17. const _this = this;
  18. _this.banned = true;
  19. _this.ban = ban;
  20. bannedCallbacks.forEach(callback => {
  21. callback(true, _this.ban);
  22. });
  23. },
  24. isBanned(cb) {
  25. if (this.ready) return cb(false);
  26. if (!this.ready && this.banned === true) return cb(true, this.ban);
  27. return bannedCallbacks.push(cb);
  28. },
  29. data(authenticated, role, username, userId) {
  30. this.authenticated = authenticated;
  31. this.role = role;
  32. this.username = username;
  33. this.userId = userId;
  34. this.ready = true;
  35. callbacks.forEach(callback => {
  36. callback(authenticated, role, username, userId);
  37. });
  38. bannedCallbacks.forEach(callback => {
  39. callback(false);
  40. });
  41. callbacks = [];
  42. }
  43. };