diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-03-22 18:10:39 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-03-22 18:10:39 +0100 |
commit | 2b5a80a0324d153b20eac7e6949cc02aea9c1c07 (patch) | |
tree | 87be8ab0dda9a504eb9fe83c6c5c360178ab3f65 /web | |
parent | 6ab8db31c38eb9bf50485bea4cfc3b09add3daad (diff) | |
download | planetwars.dev-2b5a80a0324d153b20eac7e6949cc02aea9c1c07.tar.xz planetwars.dev-2b5a80a0324d153b20eac7e6949cc02aea9c1c07.zip |
implement currentUser store
Diffstat (limited to 'web')
-rw-r--r-- | web/pw-server/src/lib/components/navbar/UserControls.svelte | 13 | ||||
-rw-r--r-- | web/pw-server/src/lib/stores/current_user.ts | 3 | ||||
-rw-r--r-- | web/pw-server/src/routes/login.svelte | 8 |
3 files changed, 14 insertions, 10 deletions
diff --git a/web/pw-server/src/lib/components/navbar/UserControls.svelte b/web/pw-server/src/lib/components/navbar/UserControls.svelte index e9dbab1..3c1d181 100644 --- a/web/pw-server/src/lib/components/navbar/UserControls.svelte +++ b/web/pw-server/src/lib/components/navbar/UserControls.svelte @@ -1,11 +1,11 @@ <script lang="ts"> import { get_session_token } from "$lib/auth"; + import { currentUser } from "$lib/stores/current_user"; import { onMount } from "svelte"; - let user = null; - onMount(async () => { + // TODO: currentUser won't be set if the navbar component is not created. const session_token = get_session_token(); if (!session_token) { return; @@ -23,19 +23,20 @@ throw response.statusText; } - user = await response.json(); + const user = await response.json(); + currentUser.set(user); }); function signOut() { // TODO: destroy session on server - user = null; + currentUser.set(null); } </script> <div class="user-controls"> - {#if user} + {#if $currentUser} <div class="current-user-name"> - {user["username"]} + {$currentUser["username"]} </div> <div class="sign-out" on:click={signOut}>Sign out</div> {:else} diff --git a/web/pw-server/src/lib/stores/current_user.ts b/web/pw-server/src/lib/stores/current_user.ts new file mode 100644 index 0000000..ac53070 --- /dev/null +++ b/web/pw-server/src/lib/stores/current_user.ts @@ -0,0 +1,3 @@ +import { writable } from "svelte/store"; + +export const currentUser = writable(null); diff --git a/web/pw-server/src/routes/login.svelte b/web/pw-server/src/routes/login.svelte index 9ff0378..911635b 100644 --- a/web/pw-server/src/routes/login.svelte +++ b/web/pw-server/src/routes/login.svelte @@ -1,6 +1,7 @@ <script lang="ts"> import { get_session_token, set_session_token } from "$lib/auth"; import { goto } from "$app/navigation"; + import { currentUser } from "$lib/stores/current_user"; let username: string | undefined; let password: string | undefined; @@ -24,7 +25,8 @@ let token = response.headers.get("Token"); set_session_token(token); - let user = await response.json(); + const user = await response.json(); + currentUser.set(user); goto("/"); } @@ -35,7 +37,6 @@ } </script> - <div class="page-card"> <div class="page-card-content"> <h1 class="page-card-header">Sign in</h1> @@ -49,7 +50,6 @@ </div> </div> - <style lang="scss"> @import "src/styles/account_forms.scss"; -</style>
\ No newline at end of file +</style> |