How to get and set simple data or a variable in a session?
I tried settings the username and then reading it in a session in the client side in the following code but it gives an error:
import { useSession } from "vinxi/http";
function getSession() {
return useSession({
password: process.env.SESSION_SECRET
});
}
export default function Index() {
const session = getSession();
session.data.user = "adam"; //set the user name
const username : string = session.data.user //get the user name
return (
<div>
<p>Username: {username}</p>
</div>
);
}
3 Replies
useSession
is for server side use (H3Event
only exists on the server side) so that component would never work on the client side.Handle Session - h3
Remember your users using a session.
Event Object - h3
Event object carries an incoming request and context.
Thanks, so what should I use for sessions on the client side?
Typically
useSession
just manages an HttpOnly
cookie which just contains a session ID so the server can recognize the client.
On the client side you could have a context which you would populate with the result from a server function or the result of an action.GitHub
solid-start/examples/with-auth/src/lib/index.ts at 678d9acbc0bb669f...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
MDN Web Docs
Set-Cookie - HTTP | MDN
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later.
To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.