Skip to content

Commit

Permalink
Login test
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Feb 2, 2024
1 parent 5dfb266 commit b64c29a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/routes/+layout.js
Original file line number Diff line number Diff line change
@@ -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();
}
14 changes: 13 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
<script>
export let data;
$: me = data.me;
</script>

<h1>Welcome to SvelteKit</h1>
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>
<p>
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation
</p>

{#if me}
<p>The current logged-in user is {me.name}</p>
{/if}

0 comments on commit b64c29a

Please sign in to comment.