#Sessions?
21 messages · Page 1 of 1 (latest)
Yeah so you can use server$, routeAction$, routeLoader$ which have access to the requestEvent, and from there you can read/write the session data
That's what I figured, but I can't find it!
tried requestEvent.session and sharedMap
https://qwik.dev/docs/server$/#accessing-request-information-with-requestevent
maybe this and this.request.session.data?
Double checking but I'm pretty sure session is undefined
Yep. undefined
I think session is dropped during createRequestEvent in the Node adapter? Not sure if that's intentional or not
const getSession = server$(async (requestEvent) => {
const session = requestEvent.request.session;
return session;
});
so this also doesn't work?
RequestEvent hangs off of this in server$, so this logs undefined
export const serverSearch = server$(async function () {
//@ts-ignore
console.log("session?", this.request.session);
Route Protection
Session data can be accessed via the route event.sharedMap. So a route can be protected and redirect using something like this located in a layout.tsx or page index.tsx:
export const onRequest: RequestHandler = (event) => {
const session: Session | null = event.sharedMap.get('session');
if (!session || new Date(session.expires) < new Date()) {
throw event.redirect(302, `/api/auth/signin?callbackUrl=${event.url.pathname}`);
}
};
I see this in the docs
export const useAction = routeAction$(
async (data, requestEvent) => {
const session = requestEvent.sharedMap.get("session");
if (!session || new Date(session.expires) < new Date()) {
return requestEvent.fail(500, {
message: "Not signed in",
});
}
this works for me
That's specfic to authjs it would seem
I have a session set up in existing Express code. The cookie is there but there is no sharedMap "session"
ahhh
you could add it to the sharedMap though when you login couldnt you?
you should have access to cookies in that same requestObject though too i believe
const session = requestEvent.cookie.get("session");
I can access the cookie for sure, but it's just the session id