Auth.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2019 Hackware SpA <human@hackware.cl>
  2. // This file is part of "Hackware Userland" and licensed under the terms
  3. // of the GNU Affero General Public License version 3, or (at your option)
  4. // a later version. You should have received a copy of this license along
  5. // with the software. If not, see <https://www.gnu.org/licenses/>.
  6. export default class Auth {
  7. constructor($fetcher) {
  8. this.user = JSON.parse(sessionStorage.getItem('user'));
  9. this.$fetcher = $fetcher;
  10. }
  11. setUser(user) {
  12. this.user = user;
  13. sessionStorage.setItem('user', JSON.stringify(this.user));
  14. }
  15. get uid() {
  16. return this.user.uid;
  17. }
  18. logout() {
  19. this.user = null;
  20. sessionStorage.removeItem('user');
  21. }
  22. isLoggedIn() {
  23. if (this.user) {
  24. return true;
  25. }
  26. return false;
  27. }
  28. fetchUser(authToken = null, processResponse = true) {
  29. const headers = authToken ? { Authorization: `Bearer ${authToken}` } : {};
  30. return this.$fetcher.haweseGet('/auth/whoami', {}, headers, processResponse);
  31. }
  32. }