blob: cbb4856556d3e877b7b7072e024cae7999022128 (
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
|
<script lang="ts">
import "../app.pcss";
import { afterNavigate, beforeNavigate, goto } from "$app/navigation";
import { is_active } from "$lib/session";
import type { Navigation } from "@sveltejs/kit";
import { setLocale } from "$lib/i18n/i18n-svelte";
import { onMount } from "svelte";
import type { LayoutData } from "./$types";
import LocaleSwitcher from "$lib/components/locale-switcher.svelte";
export let data: LayoutData;
onMount(() => setLocale(data.locale));
async function redirect_if_necessary(ticket: Navigation) {
const sessionIsValid = await is_active();
// TODO: ticket.to can be empty while navigating, so coalesce could probably cause non-public routes to cause a redir to /login...
const isPublicRoute =
ticket.to?.routeId?.startsWith("(public)") ?? true;
console.log("redir: ", {
isPublicRoute,
sessionIsValid,
});
if (sessionIsValid && isPublicRoute) {
await goto("/home");
} else if (!sessionIsValid && !isPublicRoute) {
await goto("/login");
}
}
// This should probably be removed in favor of the logic in layout.server.ts.
// That requires a more sophisticated server side implementation of session handling,
// and i don't want that tbh, i want to stay as much in the browser as possible.
afterNavigate(redirect_if_necessary);
beforeNavigate(redirect_if_necessary);
</script>
<LocaleSwitcher />
<slot />
|