aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/routes/(main)/+layout.server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'code/app/src/routes/(main)/+layout.server.ts')
-rw-r--r--code/app/src/routes/(main)/+layout.server.ts74
1 files changed, 27 insertions, 47 deletions
diff --git a/code/app/src/routes/(main)/+layout.server.ts b/code/app/src/routes/(main)/+layout.server.ts
index 4199d7f..b040b8f 100644
--- a/code/app/src/routes/(main)/+layout.server.ts
+++ b/code/app/src/routes/(main)/+layout.server.ts
@@ -1,35 +1,41 @@
-import {api_base, CookieNames} from "$configuration";
-import {log_debug, log_error} from "$help/logger";
-import {error, redirect} from "@sveltejs/kit";
-import {Temporal} from "temporal-polyfill";
-import type {LayoutServerLoad} from "./$types";
+import { api_base, CookieNames } from "$configuration";
+import { cached_result_async, CacheKeys } from "$help/cache";
+import { log_debug, log_error } from "$help/logger";
+import { md5 } from "$help/md5";
+import { error, redirect } from "@sveltejs/kit";
+import type { LayoutServerLoad } from "./$types";
-export const load: LayoutServerLoad = async ({url, request, route, cookies, locals, fetch}) => {
- console.log(url.toString());
+export const load: LayoutServerLoad = async ({ route, cookies, locals, fetch }) => {
const isBaseRoute = route.id === "/(main)";
- const isPublicRoute = (route.id?.startsWith("/(main)/(public)") || isBaseRoute) ?? true;
+ const isPortalRoute = route.id === "/(main)/(public)/portal";
+ const isPublicRoute = (isBaseRoute || (route.id?.startsWith("/(main)/(public)") ?? false)) ?? true;
const sessionCookieValue = cookies.get(CookieNames.session);
- const hasSessionCookie = (sessionCookieValue?.length > 0 ?? false);
- const sessionIsValid = hasSessionCookie && (await cached_result_async<Response>("sessionCheck", 120, () => fetch(api_base("_/is-authenticated"), {
- headers: {
- Cookie: CookieNames.session + "=" + sessionCookieValue,
- },
- }).catch((e) => {
- log_error(e);
- throw error(503, {
- message: "We are experiencing a service disruption! Have patience while we resolve the issue.",
- });
- }))).ok;
+ let sessionIsValid = false;
+ if ((sessionCookieValue?.length > 0 ?? false)) {
+ const sessionHash = md5(sessionCookieValue);
+ sessionIsValid = (await cached_result_async<Response>(sessionHash + "_" + CacheKeys.isAuthenticated, 120, () => fetch(api_base("_/is-authenticated"), {
+ headers: {
+ Cookie: CookieNames.session + "=" + sessionCookieValue,
+ },
+ }).catch((e) => {
+ log_error(e);
+ throw error(503, {
+ message: "We are experiencing a service disruption! Have patience while we resolve the issue.",
+ });
+ }))).ok;
+ }
log_debug("Base Layout loaded", {
sessionIsValid,
isPublicRoute,
+ isBaseRoute,
+ isPortalRoute,
routeId: route.id,
});
- if (sessionIsValid && isPublicRoute) {
+ if (sessionIsValid && isPublicRoute && !isPortalRoute) {
throw redirect(302, "/home");
- } else if (isBaseRoute || !sessionIsValid && !isPublicRoute) {
+ } else if (!isPortalRoute && (isBaseRoute || !sessionIsValid && !isPublicRoute)) {
throw redirect(302, "/sign-in");
}
@@ -37,29 +43,3 @@ export const load: LayoutServerLoad = async ({url, request, route, cookies, loca
locale: locals.locale,
};
};
-
-let resultCache = {};
-
-async function cached_result_async<T>(key: string, staleAfterSeconds: number, get_result: any, forceRefresh: boolean = false) {
- if (!resultCache[key]) {
- resultCache[key] = {
- l: 0,
- c: undefined as T,
- };
- }
- const staleEpoch = ((resultCache[key]?.l ?? 0) + staleAfterSeconds);
- const isStale = forceRefresh || (staleEpoch < Temporal.Now.instant().epochSeconds);
- if (isStale || !resultCache[key]?.c) {
- resultCache[key].c = await get_result();
- resultCache[key].l = Temporal.Now.instant().epochSeconds;
- }
-
- log_debug("Ran cached_result_async", {
- cacheKey: key,
- isStale,
- cache: resultCache[key],
- staleEpoch,
- });
-
- return resultCache[key].c as T;
-} \ No newline at end of file