mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-12-22 19:01:26 +00:00
parent
684662c90b
commit
7222632389
5 changed files with 23 additions and 18 deletions
|
@ -70,7 +70,7 @@
|
|||
"inferno-router": "^8.2.2",
|
||||
"inferno-server": "^8.2.2",
|
||||
"jwt-decode": "^4.0.0",
|
||||
"lemmy-js-client": "0.19.0-rc.15",
|
||||
"lemmy-js-client": "0.19.0-rc.16",
|
||||
"lodash.isequal": "^4.5.0",
|
||||
"markdown-it": "^13.0.1",
|
||||
"markdown-it-bidi": "^0.1.0",
|
||||
|
|
|
@ -20,6 +20,7 @@ import {
|
|||
import { createSsrHtml } from "../utils/create-ssr-html";
|
||||
import { getErrorPageData } from "../utils/get-error-page-data";
|
||||
import { setForwardedHeaders } from "../utils/set-forwarded-headers";
|
||||
import { authCookieName } from "../../shared/config";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
|
@ -32,7 +33,7 @@ export default async (req: Request, res: Response) => {
|
|||
);
|
||||
|
||||
const auth = req.headers.cookie
|
||||
? cookie.parse(req.headers.cookie).jwt
|
||||
? cookie.parse(req.headers.cookie)[authCookieName]
|
||||
: undefined;
|
||||
|
||||
if (auth) {
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import * as cookie from "cookie";
|
||||
import type { Request } from "express";
|
||||
import { authCookieName } from "../../shared/config";
|
||||
|
||||
export function hasJwtCookie(req: Request): boolean {
|
||||
return Boolean(cookie.parse(req.headers.cookie ?? "").jwt?.length);
|
||||
return Boolean(
|
||||
cookie.parse(req.headers.cookie ?? "")[authCookieName]?.length,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ export const updateUnreadCountsInterval = 30000;
|
|||
export const fetchLimit = 20;
|
||||
export const relTags = "noopener nofollow";
|
||||
export const emDash = "\u2014";
|
||||
export const authCookieName = "jwt";
|
||||
export const authCookieName = "auth";
|
||||
|
||||
// No. of max displayed communities per
|
||||
// page on route "/communities"
|
||||
|
|
|
@ -7,6 +7,7 @@ import { toast } from "../toast";
|
|||
import { I18NextService } from "./I18NextService";
|
||||
import { amAdmin } from "@utils/roles";
|
||||
import { HttpService } from ".";
|
||||
import { authCookieName } from "../config";
|
||||
|
||||
interface Claims {
|
||||
sub: number;
|
||||
|
@ -14,18 +15,18 @@ interface Claims {
|
|||
iat: number;
|
||||
}
|
||||
|
||||
interface JwtInfo {
|
||||
interface AuthInfo {
|
||||
claims: Claims;
|
||||
jwt: string;
|
||||
auth: string;
|
||||
}
|
||||
|
||||
export class UserService {
|
||||
static #instance: UserService;
|
||||
public myUserInfo?: MyUserInfo;
|
||||
public jwtInfo?: JwtInfo;
|
||||
public authInfo?: AuthInfo;
|
||||
|
||||
private constructor() {
|
||||
this.#setJwtInfo();
|
||||
this.#setAuthInfo();
|
||||
}
|
||||
|
||||
public login({
|
||||
|
@ -38,12 +39,12 @@ export class UserService {
|
|||
if (isBrowser() && res.jwt) {
|
||||
showToast && toast(I18NextService.i18n.t("logged_in"));
|
||||
setAuthCookie(res.jwt);
|
||||
this.#setJwtInfo();
|
||||
this.#setAuthInfo();
|
||||
}
|
||||
}
|
||||
|
||||
public logout() {
|
||||
this.jwtInfo = undefined;
|
||||
this.authInfo = undefined;
|
||||
this.myUserInfo = undefined;
|
||||
|
||||
if (isBrowser()) {
|
||||
|
@ -60,10 +61,10 @@ export class UserService {
|
|||
}
|
||||
|
||||
public auth(throwErr = false): string | undefined {
|
||||
const jwt = this.jwtInfo?.jwt;
|
||||
const auth = this.authInfo?.auth;
|
||||
|
||||
if (jwt) {
|
||||
return jwt;
|
||||
if (auth) {
|
||||
return auth;
|
||||
} else {
|
||||
const msg = "No JWT cookie found";
|
||||
|
||||
|
@ -77,13 +78,13 @@ export class UserService {
|
|||
}
|
||||
}
|
||||
|
||||
#setJwtInfo() {
|
||||
#setAuthInfo() {
|
||||
if (isBrowser()) {
|
||||
const { jwt } = cookie.parse(document.cookie);
|
||||
const auth = cookie.parse(document.cookie)[authCookieName];
|
||||
|
||||
if (jwt) {
|
||||
HttpService.client.setHeaders({ Authorization: `Bearer ${jwt}` });
|
||||
this.jwtInfo = { jwt, claims: jwtDecode(jwt) };
|
||||
if (auth) {
|
||||
HttpService.client.setHeaders({ Authorization: `Bearer ${auth}` });
|
||||
this.authInfo = { auth, claims: jwtDecode(auth) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue