aboutsummaryrefslogtreecommitdiffstats
path: root/apps/kit/src/routes/(main)/+layout.server.ts
blob: d2eb2eb72f1c605151918224138472e6cd060e6e (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 { logError } 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;

    let sessionIsValid = (await fetch(api_base("_/valid-session"), {
        headers: {
            Cookie: CookieNames.session + "=" + cookies.get(CookieNames.session)
        }
    }).catch((e) => {
        logError(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 (!sessionIsValid && !isPublicRoute) {
        throw redirect(302, "/sign-in");
    }
    return {
        locale: locals.locale
    }
};