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

export const load: LayoutServerLoad = async ({ routeId, cookies, locals }) => {
    const isPublicRoute = (routeId?.startsWith("(main)/(public)") || routeId === "(main)") ?? 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;

    console.log("Base Layout loaded", {
        sessionIsValid,
        isPublicRoute,
        routeId
    });

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

    return {
        locale: locals.locale
    }
};