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) {
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) };
}