* slight refactor, tweak params
* fix paths
* remove domain
* remove expires
* Use maxAge instead of expires
---------
Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>
export const fetchLimit = 20;
export const relTags = "noopener nofollow";
export const emDash = "\u2014";
export const fetchLimit = 20;
export const relTags = "noopener nofollow";
export const emDash = "\u2014";
+export const authCookieName = "jwt";
-// import Cookies from 'js-cookie';
import { isAuthPath } from "@utils/app";
import { isAuthPath } from "@utils/app";
-import { isBrowser } from "@utils/browser";
-import { isHttps } from "@utils/env";
+import { clearAuthCookie, isBrowser, setAuthCookie } from "@utils/browser";
import * as cookie from "cookie";
import jwt_decode from "jwt-decode";
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
import * as cookie from "cookie";
import jwt_decode from "jwt-decode";
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
public login(res: LoginResponse) {
const expires = new Date();
expires.setDate(expires.getDate() + 365);
public login(res: LoginResponse) {
const expires = new Date();
expires.setDate(expires.getDate() + 365);
if (isBrowser() && res.jwt) {
toast(I18NextService.i18n.t("logged_in"));
if (isBrowser() && res.jwt) {
toast(I18NextService.i18n.t("logged_in"));
- document.cookie = cookie.serialize("jwt", res.jwt, {
- expires,
- secure: isHttps(),
- domain: location.hostname,
- sameSite: true,
- path: "/",
- });
+ setAuthCookie(res.jwt);
public logout() {
this.jwtInfo = undefined;
this.myUserInfo = undefined;
public logout() {
this.jwtInfo = undefined;
this.myUserInfo = undefined;
- document.cookie = cookie.serialize("jwt", "", {
- maxAge: 0,
- path: "/",
- domain: location.hostname,
- sameSite: true,
- });
if (isAuthPath(location.pathname)) {
location.replace("/");
} else {
if (isAuthPath(location.pathname)) {
location.replace("/");
} else {
public auth(throwErr = false): string | undefined {
const jwt = this.jwtInfo?.jwt;
public auth(throwErr = false): string | undefined {
const jwt = this.jwtInfo?.jwt;
if (jwt) {
return jwt;
} else {
const msg = "No JWT cookie found";
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");
}
if (throwErr && isBrowser()) {
console.error(msg);
toast(I18NextService.i18n.t("not_logged_in"), "danger");
}
return undefined;
// throw msg;
}
return undefined;
// throw msg;
}
#setJwtInfo() {
if (isBrowser()) {
const { jwt } = cookie.parse(document.cookie);
#setJwtInfo() {
if (isBrowser()) {
const { jwt } = cookie.parse(document.cookie);
if (jwt) {
this.jwtInfo = { jwt, claims: jwt_decode(jwt) };
}
if (jwt) {
this.jwtInfo = { jwt, claims: jwt_decode(jwt) };
}
--- /dev/null
+import * as cookie from "cookie";
+import { authCookieName } from "../../config";
+
+export default function clearAuthCookie() {
+ document.cookie = cookie.serialize(authCookieName, "", {
+ maxAge: -1,
+ sameSite: true,
+ path: "/",
+ });
+}
import canShare from "./can-share";
import canShare from "./can-share";
+import clearAuthCookie from "./clear-auth-cookie";
import dataBsTheme from "./data-bs-theme";
import isBrowser from "./is-browser";
import isDark from "./is-dark";
import loadCss from "./load-css";
import restoreScrollPosition from "./restore-scroll-position";
import saveScrollPosition from "./save-scroll-position";
import dataBsTheme from "./data-bs-theme";
import isBrowser from "./is-browser";
import isDark from "./is-dark";
import loadCss from "./load-css";
import restoreScrollPosition from "./restore-scroll-position";
import saveScrollPosition from "./save-scroll-position";
+import setAuthCookie from "./set-auth-cookie";
import share from "./share";
export {
canShare,
import share from "./share";
export {
canShare,
dataBsTheme,
isBrowser,
isDark,
loadCss,
restoreScrollPosition,
saveScrollPosition,
dataBsTheme,
isBrowser,
isDark,
loadCss,
restoreScrollPosition,
saveScrollPosition,
--- /dev/null
+import { isHttps } from "@utils/env";
+import * as cookie from "cookie";
+import { authCookieName } from "../../config";
+
+export default function setAuthCookie(jwt: string) {
+ document.cookie = cookie.serialize(authCookieName, jwt, {
+ maxAge: 365 * 24 * 60 * 60 * 1000,
+ secure: isHttps(),
+ sameSite: true,
+ path: "/",
+ });
+}