]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/services/UserService.ts
Changing all bigints to numbers
[lemmy-ui.git] / src / shared / services / UserService.ts
index 0c87f7da715ac8a9be28bc24e279f5c2c66052f0..cc036b498e75c07ff0a8d6f9c45d72ef532d1dea 100644 (file)
@@ -2,8 +2,10 @@
 import IsomorphicCookie from "isomorphic-cookie";
 import jwt_decode from "jwt-decode";
 import { LoginResponse, MyUserInfo } from "lemmy-js-client";
-import { BehaviorSubject, Subject } from "rxjs";
+import { BehaviorSubject } from "rxjs";
 import { isHttps } from "../env";
+import { i18n } from "../i18next";
+import { isAuthPath, isBrowser, toast } from "../utils";
 
 interface Claims {
   sub: number;
@@ -11,50 +13,69 @@ interface Claims {
   iat: number;
 }
 
+interface JwtInfo {
+  claims: Claims;
+  jwt: string;
+}
+
 export class UserService {
   private static _instance: UserService;
-  public myUserInfo: MyUserInfo;
-  public claims: Claims;
-  public jwtSub: Subject<string> = new Subject<string>();
+  public myUserInfo?: MyUserInfo;
+  public jwtInfo?: JwtInfo;
   public unreadInboxCountSub: BehaviorSubject<number> =
     new BehaviorSubject<number>(0);
   public unreadReportCountSub: BehaviorSubject<number> =
     new BehaviorSubject<number>(0);
+  public unreadApplicationCountSub: BehaviorSubject<number> =
+    new BehaviorSubject<number>(0);
 
   private constructor() {
-    if (this.auth) {
-      this.setClaims(this.auth);
-    } else {
-      // setTheme();
-      console.log("No JWT cookie found.");
-    }
+    this.setJwtInfo();
   }
 
   public login(res: LoginResponse) {
     let expires = new Date();
     expires.setDate(expires.getDate() + 365);
-    IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps });
-    console.log("jwt cookie set");
-    this.setClaims(res.jwt);
+    if (res.jwt) {
+      toast(i18n.t("logged_in"));
+      IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps() });
+      this.setJwtInfo();
+    }
   }
 
   public logout() {
-    this.claims = undefined;
+    this.jwtInfo = undefined;
     this.myUserInfo = undefined;
-    // setTheme();
-    this.jwtSub.next("");
     IsomorphicCookie.remove("jwt"); // TODO is sometimes unreliable for some reason
-    document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.host;
-    console.log("Logged out.");
+    document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.hostname;
+    if (isAuthPath(location.pathname)) {
+      location.replace("/");
+    } else {
+      location.reload();
+    }
   }
 
-  public get auth(): string {
-    return IsomorphicCookie.load("jwt");
+  public auth(throwErr = true): string | undefined {
+    let jwt = this.jwtInfo?.jwt;
+    if (jwt) {
+      return jwt;
+    } else {
+      let msg = "No JWT cookie found";
+      if (throwErr && isBrowser()) {
+        console.error(msg);
+        toast(i18n.t("not_logged_in"), "danger");
+      }
+      return undefined;
+      // throw msg;
+    }
   }
 
-  private setClaims(jwt: string) {
-    this.claims = jwt_decode(jwt);
-    this.jwtSub.next(jwt);
+  private setJwtInfo() {
+    let jwt: string | undefined = IsomorphicCookie.load("jwt");
+
+    if (jwt) {
+      this.jwtInfo = { jwt, claims: jwt_decode(jwt) };
+    }
   }
 
   public static get Instance() {