auth.js 1.0 KB

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