From 2b5a80a0324d153b20eac7e6949cc02aea9c1c07 Mon Sep 17 00:00:00 2001
From: Ilion Beyst <ilion.beyst@gmail.com>
Date: Tue, 22 Mar 2022 18:10:39 +0100
Subject: implement currentUser store

---
 web/pw-server/src/lib/components/navbar/UserControls.svelte | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

(limited to 'web/pw-server/src/lib/components/navbar/UserControls.svelte')

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}
-- 
cgit v1.2.3

