mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-25 13:51:13 +00:00
fix inability to logout
This commit is contained in:
parent
26979b91c2
commit
663b5a0b99
1 changed files with 10 additions and 6 deletions
|
@ -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) };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue