Skip to content

Commit 61931bc

Browse files
committed
Add some example with routing and context
1 parent 002b849 commit 61931bc

File tree

8 files changed

+218
-134
lines changed

8 files changed

+218
-134
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import AgentLabsLogo from "$lib/components/common/logo/AgentLabsLogo.svelte";
3+
</script>
4+
5+
<div class="min-h-screen bg-background-primary flex items-center justify-center">
6+
<div class="flex flex-col animate-pulse">
7+
<AgentLabsLogo />
8+
</div>
9+
</div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script lang="ts">
2+
import { writable } from "svelte/store";
3+
import { setContext } from "svelte";
4+
5+
const userStore = writable<{ name: string; email: string } | null>(null);
6+
7+
// const auth = onUserStateChanged()...
8+
9+
const login = (email: string, password: string) => {
10+
// mocked
11+
userStore.set({
12+
name: "John",
13+
email: email
14+
});
15+
};
16+
17+
const logout = () => {
18+
userStore.set(null);
19+
};
20+
21+
setContext("Auth", { currentUser: userStore, login, logout });
22+
</script>
23+
24+
<slot />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script lang="ts">
2+
import { onMount } from "svelte";
3+
import { getContext } from "svelte";
4+
import { goto } from "$app/navigation";
5+
import LoadingFrame from "$lib/components/common/loading-frame/LoadingFrame.svelte";
6+
const { currentUser } = getContext("Auth");
7+
8+
const redirect = "/login";
9+
10+
let loading = true;
11+
12+
onMount(async () => {
13+
console.log("ICI", $currentUser);
14+
if (!$currentUser) {
15+
await goto(redirect);
16+
}
17+
loading = false;
18+
});
19+
</script>
20+
21+
{#if !loading}
22+
<slot />
23+
{:else}
24+
<LoadingFrame />
25+
{/if}

frontend/src/lib/services/.gitkeep

Whitespace-only changes.

frontend/src/routes/+layout.svelte

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<script lang="ts">
22
import type { LayoutData } from "./$types";
33
import "../app.css";
4-
import AgentLabsLogo from "$lib/components/common/logo/AgentLabsLogo.svelte";
4+
import AuthContext from "$lib/context/AuthContext.svelte";
5+
import LoadingFrame from "$lib/components/common/loading-frame/LoadingFrame.svelte";
56
67
export let data: LayoutData;
78
</script>
89

910
{#await data.lazy.context}
10-
<div class="min-h-screen bg-background-primary flex items-center justify-center">
11-
<div class="flex flex-col animate-pulse">
12-
<AgentLabsLogo />
13-
</div>
14-
</div>
11+
<LoadingFrame />
1512
{:then context}
16-
<slot />
13+
<AuthContext>
14+
<slot />
15+
</AuthContext>
1716
{/await}

frontend/src/routes/+page.svelte

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import LoadingFrame from "$lib/components/common/loading-frame/LoadingFrame.svelte";
3+
import { onMount } from "svelte";
4+
import { goto } from "$app/navigation";
5+
export const csr = true;
6+
7+
onMount(() => {
8+
goto("/main");
9+
});
10+
</script>
11+
12+
<LoadingFrame />

frontend/src/routes/login/+page.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import AuthProviderButton from "$lib/components/auth/button/AuthProviderButton.svelte";
55
import { z } from "zod";
66
7+
import { getContext } from "svelte";
8+
import { goto } from "$app/navigation";
9+
const { login, currentUser } = getContext("Auth");
10+
711
const form = {
812
email: {
913
value: "",
@@ -19,7 +23,9 @@
1923
2024
let isLoading = false;
2125
22-
const onSubmit = () => {
26+
const onSubmit = async (event) => {
27+
event.preventDefault();
28+
2329
isLoading = true;
2430
const result = formSchema.safeParse({
2531
email: form.email.value
@@ -28,6 +34,9 @@
2834
if (!result.success) {
2935
form.email.error = result.error.errors[0].message;
3036
} else {
37+
login("John Doe", "pass");
38+
console.log("goto!", $currentUser);
39+
await goto("/main");
3140
form.email.error = "";
3241
}
3342
isLoading = false;

0 commit comments

Comments
 (0)