fix inability to logout

This commit is contained in:
Alec Armbruster 2023-07-04 09:14:47 -04:00
parent 26979b91c2
commit 663b5a0b99
No known key found for this signature in database
GPG key ID: 52BC7C84E960FD1B

View file

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