diff --git a/src/shared/services/UserService.ts b/src/shared/services/UserService.ts index 3757e236..d2a53647 100644 --- a/src/shared/services/UserService.ts +++ b/src/shared/services/UserService.ts @@ -31,8 +31,10 @@ export class UserService { public login(res: LoginResponse) { const expires = new Date(); expires.setDate(expires.getDate() + 365); + if (isBrowser() && res.jwt) { toast(I18NextService.i18n.t("logged_in")); + document.cookie = cookie.serialize("jwt", res.jwt, { expires, secure: isHttps(), @@ -40,6 +42,7 @@ export class UserService { sameSite: true, path: "/", }); + this.#setJwtInfo(); } } @@ -47,14 +50,11 @@ export class UserService { public logout() { this.jwtInfo = undefined; this.myUserInfo = undefined; + if (isBrowser()) { - document.cookie = cookie.serialize("jwt", "", { - maxAge: 0, - path: "/", - domain: location.hostname, - sameSite: true, - }); + document.cookie = ""; } + if (isAuthPath(location.pathname)) { location.replace("/"); } else { @@ -64,14 +64,17 @@ export class UserService { public auth(throwErr = false): string | undefined { const jwt = this.jwtInfo?.jwt; + if (jwt) { return jwt; } else { const msg = "No JWT cookie found"; + if (throwErr && isBrowser()) { console.error(msg); toast(I18NextService.i18n.t("not_logged_in"), "danger"); } + return undefined; // throw msg; } @@ -80,6 +83,7 @@ export class UserService { #setJwtInfo() { if (isBrowser()) { const { jwt } = cookie.parse(document.cookie); + if (jwt) { this.jwtInfo = { jwt, claims: jwt_decode(jwt) }; }