sidebase / nuxt-session

Nuxt session middleware to get a persistent session per app user, e.g., to store data across multiple requests. The nuxt session module provides the useSession() composable out of the box and sets up API endpoints to interact with your session to make working with sessions feel like a breeze.
https://sidebase.io/nuxt-session/
MIT License
188 stars 19 forks source link

Is it by design that the session during the first server processing is not saved in storage? I'm guessing it's a bug. #89

Open dogharrycatpotter opened 1 year ago

dogharrycatpotter commented 1 year ago

Ask your question

The following conditions occur:

・There is a cookie that holds the session ID set in the HTTP request. ・The session on the server no longer exists on the storage ・Initial server access

During server processing, event.context.session.user = { name: 'abc' } is set to the session, but it is not saved on the server's storage.

The results of debugging index.mjs are below.

export default eventHandler(async (event) => {
  console.log(`@sidebase/nuxt-session eventHandler start`);
  await ensureSession(event);
  event.res.on("finish", async () => {
    const session = await getSession(event);
    console.log(`@sidebase/nuxt-session eventHandler end session`, session); // $1
    if (!session) {
      return;
    }
    await setStorageSession(session.id, event.context.session);
  });
});

The $1 comment line above is output as null, so it seems that the storage is not saved.

const getCurrentSessionId = (event) => {
  const sessionIdRequest = parseCookies(event).sessionId;
  const sessionIdContext = event.context.sessionId;
  console.log(`@sidebase/nuxt-session getCurrentSessionId sessionIdRequest`, sessionIdRequest); // $2
  console.log(`@sidebase/nuxt-session getCurrentSessionId sessionIdContext`, sessionIdContext); // $3
  if (sessionIdContext && sessionIdRequest && sessionIdContext !== sessionIdRequest) {
    return null;
  }
  return sessionIdRequest || sessionIdContext || null;
};

When I debug the getSession function, I see that the getCurrentSessionId function returns null.

The output content of the $2,3 comment line above is as follows.

@sidebase/nuxt-session getCurrentSessionId sessionIdRequest k2XJZcDXmKxtoAzbJIIM0sNr9piseVF1
@sidebase/nuxt-session getCurrentSessionId sessionIdContext nIHE9cPHVNaFwLtonucpZn6ANWVOS8F2

The version I'm using is below. "@sidebase/nuxt-session": "^0.2.7"

Additional information

No response

agracia-foticos commented 10 months ago

Same issue!