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

Question: maxAge druring session updates #342

Open
Yizack opened this issue Feb 5, 2025 · 0 comments
Open

Question: maxAge druring session updates #342

Yizack opened this issue Feb 5, 2025 · 0 comments

Comments

@Yizack
Copy link
Contributor

Yizack commented Feb 5, 2025

Hello Atinux, I wanted to ask if the current behavior when overwriting maxAge and then maxAge is lost after updating the session later is intended

Summary

I tried to implement a "Remember me" option when logging in doing something like

// server/api/login.post.ts
await setUserSession(event, {
  user: {
    // user props
  },
}, {
  maxAge: remember ? 60 * 60 * 24 * 7 : undefined // if remember is true, maxAge is 7 days
})

It does overwrite the default maxAge as shown in the screenshot

Image

However, after updating the session (for example to change the name of the user) with the following code, the maxAge is lost and changes to Session while I expected it to be the same above

// server/api/user.put.ts
await setUserSession(event, {
  user: {
    name: 'Test'
  }
})

Image

Solution

To solve this at first I thought to keep the remember option as a session property but that would mean I'd need to check it and set maxAge in all setUserSession of the app, so instead I ended up editing the setUserSession util to include a maxAge property and check if the current session data includes maxAge and a new config maxAge is not set

export async function setUserSession(event: H3Event, data: UserSession, config?: Partial<SessionConfig>) {
  let session = await _useSession(event, config)

  if (ssession.data.maxAge && config?.maxAge === undefined) {
    session = await _useSession(event, {
      ...config,
      maxAge: session.data.maxAge as number,
    })
  }

  data.maxAge = config?.maxAge
  await session.update(defu(data, session.data))

  return session.data
}

See my changes here main...Yizack:nuxt-auth-utils:fix/max-age#diff-924ff08bed63f380edb066147815aa363d2115f9b770185d9ba7c01dbd327a1a

I also included an update button to test in the playground, I'm happy to include the steps for testing in a PR if you think this is a good idea :), if the behavior is intended we can close this issue

@Yizack Yizack changed the title Question: maxAge drurint session updates Question: maxAge druring session updates Feb 5, 2025
@Yizack Yizack closed this as not planned Won't fix, can't repro, duplicate, stale Feb 5, 2025
@Yizack Yizack reopened this Feb 6, 2025
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

1 participant