auth.js 947 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. getStatus: function (cb) {
  11. if (this.ready) cb(this.authenticated, this.role, this.username, this.userId);
  12. else callbacks.push(cb);
  13. },
  14. setBanned: function() {
  15. this.banned = true;
  16. bannedCallbacks.forEach(callback => {
  17. callback(true);
  18. });
  19. },
  20. isBanned: function(cb) {
  21. if (this.ready) return cb(false);
  22. if (!this.ready && this.banned === true) return cb(true);
  23. bannedCallbacks.push(cb);
  24. },
  25. data: function (authenticated, role, username, userId) {
  26. this.authenticated = authenticated;
  27. this.role = role;
  28. this.username = username;
  29. this.userId = userId;
  30. this.ready = true;
  31. callbacks.forEach(callback => {
  32. callback(authenticated, role, username, userId);
  33. });
  34. bannedCallbacks.forEach(callback => {
  35. callback(false);
  36. });
  37. callbacks = [];
  38. }
  39. }