]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/services/UserService.ts
First pass at v2_api
[lemmy-ui.git] / src / shared / services / UserService.ts
index 72066ad4778686943d4d0fb61092ff779bc686a9..391a5ef2a7a78f57a3ef4d100fe7f3379cddb37b 100644 (file)
@@ -1,8 +1,10 @@
 // import Cookies from 'js-cookie';
 import IsomorphicCookie from 'isomorphic-cookie';
-import { User, LoginResponse } from 'lemmy-js-client';
+import { User_, LoginResponse } from 'lemmy-js-client';
 import jwt_decode from 'jwt-decode';
 import { Subject, BehaviorSubject } from 'rxjs';
+import { i18n } from '../i18next';
+import { toast } from '../utils';
 
 interface Claims {
   id: number;
@@ -11,7 +13,7 @@ interface Claims {
 
 export class UserService {
   private static _instance: UserService;
-  public user: User;
+  public user: User_;
   public claims: Claims;
   public jwtSub: Subject<string> = new Subject<string>();
   public unreadCountSub: BehaviorSubject<number> = new BehaviorSubject<number>(
@@ -48,6 +50,15 @@ export class UserService {
     return IsomorphicCookie.load('jwt');
   }
 
+  public authField(throwErr: boolean = true): string {
+    if (this.auth == null && throwErr) {
+      toast(i18n.t('not_logged_in'), 'danger');
+      throw 'Not logged in';
+    } else {
+      return this.auth;
+    }
+  }
+
   private setClaims(jwt: string) {
     this.claims = jwt_decode(jwt);
     this.jwtSub.next(jwt);