Skip to content

server plugin doesn't update server session #314

@kleinpetr

Description

@kleinpetr

Hi,

I am facing an interesting situation, where I use listen to a hook in the nitro plugin to extend session by user data, currently I have a custom UserSession interface which includes user and data fields.

When user is logged in (for example over SSO) I set just data which includes only session data such as userId, tokens, expirations, etc.

Then I have this plugin

sessionHooks.hook('fetch', async (session, event) => {
    if (!session.data) return

    const authSession = await useDrizzle().query.sessions.findFirst({
      where: eq(tables.sessions.id, session.data.id),
    })

    if (!authSession) {
      console.error('Session not found')
      clearUserSession(event)
      return sendRedirect(event, '/')
    }

    const user = await useDrizzle().query.users.findFirst({
      where: eq(tables.users.id, session.data.userId),
    })

    if (!user) {
      console.error('User not found')
      clearUserSession(event)
      return sendRedirect(event, '/')
    }

    await setUserSession(event, {
      user: user,
    })
  })

Based on the session I get current user data, to keep fresh data.

But now I have full session on the client side, when I debug the entire session comes from useUserSession() it contains both data and user

but when I debug const session = await getUserSession(event) on the server side, it contains only data which was originally set after login.

Interesting thing is, when I set something like setUserSession(event, {data, user: {test: 'test'}) after the login. Then I can see data and user objects on the client side again, but the user has also test property. But when I debug server session, it includes data again and the user as well but only with the test property.

Thank you for clarification

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions