Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to modify a session in from hook #261

Closed
genu opened this issue Oct 26, 2024 · 2 comments
Closed

Unable to modify a session in from hook #261

genu opened this issue Oct 26, 2024 · 2 comments

Comments

@genu
Copy link

genu commented Oct 26, 2024

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?

@atinux
Copy link
Owner

atinux commented Oct 27, 2024

Hi @genu

This hook is only used to populate the session for the client-side with dynamic data but it cannot update the cookie, only the returned value for useUserSession() on the app-side.

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

This is a bit related to #255, I would suggest a server middleware that can update the session.

@genu
Copy link
Author

genu commented Oct 28, 2024

Thanks @atinux I moved the above to a middleware, and it seems to get the job done.

I appreciate it.

@genu genu closed this as completed Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants