Skip to content

Unable to modify a session in from hook #261

Closed
@genu

Description

@genu

I'm running into an issue where, where I'm unable to modify a session from inside of the fetch hook.

Here is an example.

export default defineNitroPlugin(() => {
  sessionHooks.hook("fetch", async (session, event) => {
    const { getSASUrl } = $storage()

    // Update the SAS Token expiration, and get a new SAS token if it's expired
    const isTokenExpired = dayjs(session.SAS.expiresOn).isBefore(dayjs())

    if (isTokenExpired) {
      const { url, expiresOn } = await getSASUrl(
        session.activeTeam.id,
        StorageContainer.UserUploads,
      )
      session.SAS = {
        url,
        expiresOn,
      }
    }
})

when this hook runs in SSR context its able to get a updated SAS token based on the current expiresOn, however, when I refresh the page, the old cookie value is present.

Although the session is updated for the request, the cookie itself isn't updated, so the isTokenExpired above will always true because the session passed in will always be the session from the cookie.

I hope that makes sense.

is there a a proper way to update a session without having the user Logout and Log back in?

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