You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Behind the scenes, Sapper handles [de]serialization.
This is the accepted way to handle problems related to authentication, because user data has a couple of important characteristics:
You really don't want to accidentally leak it between two sessions on the same server, and generating the store on a per-request basis makes that very unlikely
It's often used in lots of different places in your app, so a global store makes sense.
But Svelte 3 doesn't have global stores that are passed around in quite the same way. One option would be to generate props, and rely on the developer to pass them around as necessary (including via layout components). This would be cumbersome, and would encourage developers to populate stores from inside components, which makes accidental data leakage significantly more likely.
A second option is to create the store on the generated app:
// generated codeimport{writable}from'svelte/store';exportconststore=writable(__SAPPER__.store);// serialized and included on the page
// anywhere inside your app
<script>import{store}asappfrom'@sapper/app';// see #551</script>
{#if $store.user}
<h1>Hello {$store.user.name}!</h1>
{:else}
<p>please <ahref="login">log in</a></p>
{/if}
Just realised this doesn't actually work. If store is just something exported by the app, there's no way to prevent leakage.
Instead, it needs to be tied to rendering, which means we need to use the context API. Sapper needs to provide a top level component that sets the store as context for the rest of the app. You would therefore only be able to access it during initialisation, which means you couldn't do it inside a setTimeout and get someone else's session by accident:
<script>import*asappfrom'@sapper/app';// this works...conststore=app.session();// but this would failsetTimeout(()=>{conststore=app.session();});</script>
Currently, with Svelte 2, you can pass data from server to client like so:
Behind the scenes, Sapper handles [de]serialization.
This is the accepted way to handle problems related to authentication, because user data has a couple of important characteristics:
But Svelte 3 doesn't have global stores that are passed around in quite the same way. One option would be to generate props, and rely on the developer to pass them around as necessary (including via layout components). This would be cumbersome, and would encourage developers to populate stores from inside components, which makes accidental data leakage significantly more likely.
A second option is to create the store on the generated app:
(See #551 for an explanation of
@sapper/app
.)Does that make sense? Is
store
the right name? (session
?)The text was updated successfully, but these errors were encountered: