From b64c29a689d0f20ac6107563c98ed103c72f17e6 Mon Sep 17 00:00:00 2001 From: Chris Amico Date: Fri, 2 Feb 2024 10:48:01 -0500 Subject: [PATCH] Login test --- src/routes/+layout.js | 25 ++++++++++++++++++++++++- src/routes/+page.svelte | 14 +++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) 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 @@ + +

Welcome to SvelteKit

-

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}