Bladeren bron

fix: some browsers cap cookie expiration date to one week, so refresh this every time a user visits the site

Kristian Vos 1 jaar geleden
bovenliggende
commit
a4d12192b8
2 gewijzigde bestanden met toevoegingen van 29 en 0 verwijderingen
  1. 4 0
      frontend/src/main.ts
  2. 25 0
      frontend/src/stores/userAuth.ts

+ 4 - 0
frontend/src/main.ts

@@ -363,6 +363,10 @@ createSocket().then(async socket => {
 			userId
 		});
 
+		if (loggedIn) {
+			userAuthStore.resetCookieExpiration();
+		}
+
 		if (configStore.experimental.media_session) ms.initialize();
 		else ms.uninitialize();
 	});

+ 25 - 0
frontend/src/stores/userAuth.ts

@@ -300,6 +300,31 @@ export const useUserAuthStore = defineStore("userAuth", {
 					resolve(this.permissions);
 				});
 			});
+		},
+		resetCookieExpiration() {
+			const cookies = {};
+			document.cookie.split("; ").forEach(cookie => {
+				cookies[cookie.substring(0, cookie.indexOf("="))] =
+					cookie.substring(cookie.indexOf("=") + 1, cookie.length);
+			});
+
+			const configStore = useConfigStore();
+			const SIDName = configStore.cookie;
+
+			if (!cookies[SIDName]) return;
+
+			const date = new Date();
+			date.setTime(new Date().getTime() + 2 * 365 * 24 * 60 * 60 * 1000);
+
+			const secure = configStore.urls.secure ? "secure=true; " : "";
+
+			let domain = "";
+			if (configStore.urls.host !== "localhost")
+				domain = ` domain=${configStore.urls.host};`;
+
+			document.cookie = `${configStore.cookie}=${
+				cookies[SIDName]
+			}; expires=${date.toUTCString()}; ${domain}${secure}path=/`;
 		}
 	}
 });