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

Merge fields instead of overwriting whole doc #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

yuvalkarmi
Copy link

I've discovered that the updateUser method of the adapter in Firestore overwrites the record if it already exists. This results in user information being lost upon update. For example, when logging in with the email provider.

Right now the bug is this:

  1. User collection has the following record:
{
      id: '12345'
      emailVerified: <some timestamp>
      email: '[email protected]'
      image: <some image>
      name: 'John Smith'
}
  1. User logs in with email, and as a consequence, next-auth calls the updateUser method of the adapter with the data:
{
      id: '12345'
      emailVerified: <some NEWER timestamp>
}
  1. Record gets overwritten completely and is now:
{
      id: '12345'
      emailVerified: <some NEWER timestamp>
}

This pull request sets the merge flag on the firebase set command to true so that the record isn't overwritten.

However, this isn't enough, because next-auth expects to receive the updated record including the original fields, which means we need to somehow get the original fields (otherwise, the session won't include the user's profile).

Unfortunately, the firebase javascript SDK (unlike the REST API) does not return the updated record in response to set or create calls (see here). As a consequence, we must make an additional call to retrieve the updated record, and then return that to next-auth.

This pull request takes care of both of these changes. Please keep in mind that I have not updated any other methods or anything outside of the firestore database (if the firebase file needs to be updated as well, this should be done, too. However, I am unfamiliar with it).

@fronterior fronterior self-assigned this Sep 5, 2022
@fronterior
Copy link
Owner

First of all, thank you for writing the PR.

Could you write in detail how to reproduce the problem?

In my test, the data was not erased when I entered a new field in the user of Firestore in the current version, logged out, and logged in again.

For your information, there is a conflict because there is a mistake in the current version update, and this will be resolved manually, so please don't worry.

fronterior
fronterior previously approved these changes Sep 6, 2022
/web/
/helper.d.ts
/*.js
/*.ts
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The *.js file is the result of the build. So it's right to be included in the .gitignore list. So I think all the changes to this commit are inappropriate.
I used the method of building in the root folder for the purpose of using alias when distributing to npm, but I will modify it to distribute by folder using tools like chakra-ui later.

@fronterior fronterior self-requested a review September 6, 2022 16:36
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

Successfully merging this pull request may close these issues.

2 participants