diff --git a/src/routes/+layout.js b/src/routes/+layout.js index 50f609ec1..c61474af4 100644 --- a/src/routes/+layout.js +++ b/src/routes/+layout.js @@ -1,13 +1,36 @@ import { locale, waitLocale } from "svelte-i18n"; import { browser } from "$app/environment"; +import { BASE_API_URL } from "@/config/config.js"; import "$lib/i18n/index.js"; // Import to initialize. Important :) export const trailingSlash = "always"; +const endpoint = new URL("users/me/", BASE_API_URL); + /** @type {import('./$types').LayoutLoad} */ -export async function load() { +export async function load({ fetch }) { if (browser) { locale.set(window.navigator.language); } await waitLocale(); + + const me = await getMe(fetch); + + return { me }; +} + +/** + * Get the current logged-in user, or null + * + * @param {fetch} fetch + * @return {*} + */ +async function getMe(fetch) { + const resp = await fetch(endpoint, { credentials: "include" }); + + if (!resp.ok) { + return null; + } + + return resp.json(); } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 5982b0ae3..7bda76e19 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,2 +1,14 @@ + +
Visit kit.svelte.dev to read the documentation
++ Visit kit.svelte.dev to read the documentation +
+ +{#if me} +The current logged-in user is {me.name}
+{/if}