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

setUserSession() is merging data #42

Closed
maximilianmikus opened this issue Jan 25, 2024 · 4 comments · Fixed by #44
Closed

setUserSession() is merging data #42

maximilianmikus opened this issue Jan 25, 2024 · 4 comments · Fixed by #44
Labels
question Further information is requested

Comments

@maximilianmikus
Copy link
Contributor

The naming of the function setUserSession suggest you can set data to what you provide as an argument. In fact new data is always merged with old session data.

/**
 * Set a user session
 * @param event
 * @param data User session data, please only store public information since it can be decoded with API calls
 */
export async function setUserSession (event: H3Event, data: UserSession) {
  const session = await _useSession(event)

  await session.update(defu(data, session.data))

  return session.data
}

defu(data, session.data) merges data deeply. If you want to actually delete data, you cannot do it. In addition to this session.update will perform an object.assign call (shallow merge).

I think we should have different functions for setting and updating(merging) session data.

@atinux
Copy link
Owner

atinux commented Jan 29, 2024

I understand your concern @maximilianmikus

So far I decided to merge the data as it is very useful for user to connect multiple providers for a specific session (see playground).

Maybe we could introduce a replaceSession(event, data) util, what do you think?

@atinux atinux added the question Further information is requested label Jan 29, 2024
@maximilianmikus
Copy link
Contributor Author

sounds good!

@atinux What do you think about renaming setUserSession to mergeUserSession(or equivalent) and use setUserSession instead of replaceSession?

if that is too big of a change. I think its fine to add replaceSession() for now.

Copy link
Owner

atinux commented Jan 29, 2024

This will be a breaking chance, I rather have replaceSession() instead :)

Feel free to open a PR for it!

@maximilianmikus
Copy link
Contributor Author

ok will do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants