aboutsummaryrefslogtreecommitdiffstats
path: root/code/app/src/routes/(main)/+layout.server.ts
blob: acb9c22ffefebee5cdce0425071894b16b5cb8e7 (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
import { api_base, CookieNames } from "$lib/configuration";
import { log_debug, log_error } from "$lib/logger";
import { error, redirect } from "@sveltejs/kit";
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 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 distruption! 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
    }
};