Replies: 4 comments 5 replies
-
https://www.captaincodeman.com/re-creating-the-sveltekit-session-store |
Beta Was this translation helpful? Give feedback.
-
At the moment I have such thoughts:
The CaptainCodeman session is readable and works only in the browser. My version: CodeThe session can be changed on the client. To update it from the server, you need to use //stores.ts
import type { Writable } from "svelte/store";
import { writable } from "svelte/store";
import { setContext, getContext } from "svelte";
import { browser } from "$app/environment";
function safe_writable<T>(key: string, initial: T): Writable<T> {
let link: Writable<T> | undefined;
function get() {
if (browser) {
if (!link) link = setContext(key, writable(initial));
return link;
} else {
return (
getContext<Writable<T>>(key) ||
setContext(key, writable(initial))
);
}
}
return {
subscribe: (x, y) => get().subscribe(x, y),
set: (x) => get().set(x),
update: (x) => get().update(x),
};
}
export let session = safe_writable<User | undefined>("session", undefined); //+layout.server.ts
import type { LayoutServerLoad } from "./$types";
export const load: LayoutServerLoad = async function (event) {
return {
user: event.locals.user,
};
}; //+layout.ts
import type { LayoutLoad } from "./$types";
export const load: LayoutLoad = async function ({ data }) {
return {
session: data.user,
};
}; //top of layout.svelte
import type { PageData } from "./$types";
export let data: PageData;
$: $session = data.session; Testhttps://stackblitz.com/edit/sveltejs-kit-template-default-arkk5k?file=src/lib/stores.ts So, there is session1 - this is a regular store, there is session2 - this is my store.
|
Beta Was this translation helpful? Give feedback.
-
Joy of code updated sveltekit authentication using cookies github repo. In this repo you can check the code and understand it, it's perfectly working. |
Beta Was this translation helpful? Give feedback.
-
I made another way that seems clearer and more logical: function writable_from_page<T>(
getter: ($page: Page) => T
): Readable<T> & { set: (value: T) => void } {
let writable_session = writable<T>();
let derived_session = derived(
[page, writable_session],
([$page, $session]) => $session || getter($page)
);
return {
set(value: T) {
if (browser) {
writable_session.set(value);
}
},
subscribe: derived_session.subscribe,
};
}
export const session = writable_from_page<User | undefined>(
($page) => $page.data.session
); You can write a value to it and it will be used until it becomes |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How do I replace the session after the update?
There are several options offered in the comments to #5883 (reply in thread), but are not quite correct.
+layout.server.js
and+layout.svelte
I can create a store in a file, set it in
+layout.server.js
and import it in other places, but there is a problem with the ssr (#4339).I can store the store in context, but the context can only be set in
+layout.svelte
, and I cannot export it inscript context="module"
or import it in+layout.server
.I can set the value in
+layout.server
only ifbrowser == true
, but then the benefits of ssr are lost.Beta Was this translation helpful? Give feedback.
All reactions