Adding option types 2 (#689)
* Not working, because of wrong API types. * Adding Rust-style Result and Option types. - Fixes #646 * Updating to use new lemmy-js-client with Options.
This commit is contained in:
parent
d41e19f3f1
commit
d905c91e1b
60 changed files with 5883 additions and 4485 deletions
|
@ -1,9 +1,12 @@
|
|||
// import Cookies from 'js-cookie';
|
||||
import { Err, None, Ok, Option, Result, Some } from "@sniptt/monads";
|
||||
import IsomorphicCookie from "isomorphic-cookie";
|
||||
import jwt_decode from "jwt-decode";
|
||||
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
|
||||
import { BehaviorSubject, Subject } from "rxjs";
|
||||
import { isHttps } from "../env";
|
||||
import { i18n } from "../i18next";
|
||||
import { isBrowser, toast } from "../utils";
|
||||
|
||||
interface Claims {
|
||||
sub: number;
|
||||
|
@ -11,11 +14,16 @@ 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: Option<MyUserInfo> = None;
|
||||
public jwtInfo: Option<JwtInfo> = None;
|
||||
public jwtSub: Subject<Option<JwtInfo>> = new Subject<Option<JwtInfo>>();
|
||||
public unreadInboxCountSub: BehaviorSubject<number> =
|
||||
new BehaviorSubject<number>(0);
|
||||
public unreadReportCountSub: BehaviorSubject<number> =
|
||||
|
@ -24,12 +32,7 @@ export class UserService {
|
|||
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) {
|
||||
|
@ -37,26 +40,42 @@ export class UserService {
|
|||
expires.setDate(expires.getDate() + 365);
|
||||
IsomorphicCookie.save("jwt", res.jwt, { expires, secure: isHttps });
|
||||
console.log("jwt cookie set");
|
||||
this.setClaims(res.jwt);
|
||||
this.setJwtInfo();
|
||||
}
|
||||
|
||||
public logout() {
|
||||
this.claims = undefined;
|
||||
this.myUserInfo = undefined;
|
||||
// setTheme();
|
||||
this.jwtSub.next("");
|
||||
this.jwtInfo = None;
|
||||
this.myUserInfo = None;
|
||||
this.jwtSub.next(this.jwtInfo);
|
||||
IsomorphicCookie.remove("jwt"); // TODO is sometimes unreliable for some reason
|
||||
document.cookie = "jwt=; Max-Age=0; path=/; domain=" + location.host;
|
||||
location.reload(); // TODO may not be necessary anymore
|
||||
console.log("Logged out.");
|
||||
}
|
||||
|
||||
public get auth(): string {
|
||||
return IsomorphicCookie.load("jwt");
|
||||
public auth(throwErr = true): Result<string, string> {
|
||||
// Can't use match to convert to result for some reason
|
||||
let jwt = this.jwtInfo.map(j => j.jwt);
|
||||
if (jwt.isSome()) {
|
||||
return Ok(jwt.unwrap());
|
||||
} else {
|
||||
let msg = "No JWT cookie found";
|
||||
if (throwErr && isBrowser()) {
|
||||
console.log(msg);
|
||||
toast(i18n.t("not_logged_in"), "danger");
|
||||
}
|
||||
return Err(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private setClaims(jwt: string) {
|
||||
this.claims = jwt_decode(jwt);
|
||||
this.jwtSub.next(jwt);
|
||||
private setJwtInfo() {
|
||||
let jwt = IsomorphicCookie.load("jwt");
|
||||
|
||||
if (jwt) {
|
||||
let jwtInfo: JwtInfo = { jwt, claims: jwt_decode(jwt) };
|
||||
this.jwtInfo = Some(jwtInfo);
|
||||
this.jwtSub.next(this.jwtInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public static get Instance() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue