aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/routes/(main)/+layout.server.ts
blob: 086d1c0efe96cba019c2b43519d126e8ad46fea6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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";

export const load: LayoutServerLoad = async ({route, cookies, locals}) => {
    const isBaseRoute = route.id === "/(main)";
    const isPublicRoute = (route.id?.startsWith("/(main)/(public)") || isBaseRoute) ?? true;
    const sessionIsValid = (await cached_result<Response>("sessionCheck", 120, () => fetch(api_base("_/valid-session"), {
        headers: {
            Cookie: CookieNames.session + "=" + cookies.get(CookieNames.session),
        },
    }).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,
        routeId: route.id,
    });

    if (sessionIsValid && isPublicRoute) {
        throw redirect(302, "/home");
    } else if (isBaseRoute || !sessionIsValid && !isPublicRoute) {
        throw redirect(302, "/sign-in");
    }

    return {
        locale: locals.locale,
    };
};

let resultCache = {};

async function cached_result<T>(key: string, staleAfterSeconds: number, code: any) {
    if (!resultCache[key]) {
        resultCache[key] = {
            l: 0,
            c: undefined as T,
        };
    }
    const staleEpoch = ((resultCache[key]?.l ?? 0) + staleAfterSeconds);
    const isStale = staleEpoch < Temporal.Now.instant().epochSeconds;
    if (isStale || !resultCache[key]?.c) {
        resultCache[key].c = await code();
        resultCache[key].l = Temporal.Now.instant().epochSeconds;
    }

    log_debug("Ran cached_result", {
        cacheKey: key,
        isStale,
        cache: resultCache[key],
        staleEpoch,
    });

    return resultCache[key].c as T;
}