In a sveltekit application, I'm using the store to save a user email after success logged in. The store seems to work in the layout but not in pages. Can someone explain me what I'm doing wrong ? Moreover, I noticed that when I save the file in VS Code the HMR makes the store in +page.svelte work, but when I manually reload the page the store goes back to undefined.Thanks for your help !
Here's my stores.js file :
// lib/stores.jsimport { writable } from 'svelte/store';export const userEmail = writable(null);
Here's the layout where I manage the connection (with Firebase) and where I set the store value :
// +layout.svelte<script> import { userEmail } from "$lib/stores.js"; import { onMount } from "svelte"; import { auth } from "$lib/firebase/fbInit"; onMount(() => { auth.onAuthStateChanged(async (user) => { if (user) { await userEmail.set(user.email); console.log($userEmail +" is connected"); // ----- The store works here and shows the user email } else { console.log("no user connected"); } }); });</script>
And here's the page example where I want to access the store value :
// examplepage/+page.svelte<script> import { userEmail } from "$lib/stores.js"; import { onMount } from "svelte"; onMount(async () => { console.log("Email in store", $userEmail); // ----- But not working here : shows undefined (but the layout console.log still shows the right user email yet) });</script>