#Sessions?

21 messages · Page 1 of 1 (latest)

dreamy pumice
#

I have existing code that uses Express with Passport.js for logins, which means my server-side code needs access to the Passport session.

Is there any way to read/write session data in Qwik server-side code?

ancient iris
dreamy pumice
#

That's what I figured, but I can't find it!

#

tried requestEvent.session and sharedMap

ancient iris
dreamy pumice
#

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

ancient iris
#
const getSession = server$(async (requestEvent) => {
  const session = requestEvent.request.session;
  return session;
});

so this also doesn't work?

dreamy pumice
#

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);
ancient iris
#
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

glad scroll
#
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

dreamy pumice
#

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"

glad scroll
#

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");
dreamy pumice
#

I can access the cookie for sure, but it's just the session id