-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |