auth.js 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. this.banned = true;
  18. this.ban = ban;
  19. bannedCallbacks.forEach(callback => {
  20. callback(true, this.ban);
  21. });
  22. },
  23. isBanned(cb) {
  24. if (this.ready) return cb(false);
  25. if (!this.ready && this.banned === true) return cb(true, this.ban);
  26. return bannedCallbacks.push(cb);
  27. },
  28. data(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. };