Skip to content

Invoke RemoteStore's credential change listener even when user didn't change #4189

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

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-actors-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixes an issue that prevented the SDK from automatically retrieving custom User claims.
20 changes: 11 additions & 9 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ export class FirestoreClient {
) {
this.credentials.setChangeListener(user => {
logDebug(LOG_TAG, 'Received user=', user.uid);
if (!this.user.isEqual(user)) {
this.user = user;
this.credentialListener(user);
}
this.user = user;
this.credentialListener(user);
this.receivedInitialUser.resolve();
});
}
Expand Down Expand Up @@ -199,11 +197,15 @@ export async function setOfflineComponentProvider(
const configuration = await client.getConfiguration();
await offlineComponentProvider.initialize(configuration);

client.setCredentialChangeListener(user =>
client.asyncQueue.enqueueRetryable(async () => {
await handleUserChange(offlineComponentProvider.localStore, user);
})
);
let currentUser = configuration.initialUser;
client.setCredentialChangeListener(user => {
if (!currentUser.isEqual(user)) {
currentUser = user;
client.asyncQueue.enqueueRetryable(async () => {
await handleUserChange(offlineComponentProvider.localStore, user);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the user change guard for LocalStore's handleUserChange here. It has always been part of the SyncEngine's call into LocalStore (https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/src/core/sync_engine.ts#L1084). This part of the code handles the user change when SyncEngine is not yet loaded (for a fully offline client).

I do however think that the guard here doesn't make a huge difference and mostly added it to be technically correct. This line of code is only executed when memory persistence is used without network access. The only sensible use case for this is an offline Firestore experience that is backed by Bundles. Bundles don't contain any mutations, so the user change would always be a no-op here. Once the user adds the first mutation, SyncEngine gets loaded and overwrites this listener.

I probably didn't describe this very well. But at least I tried, right? :)

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not unreasonable to have here. The change is queued vs being immediately applied and also provides a tiny bit more predictability to the system.

});
}
});

// When a user calls clearPersistence() in one client, all other clients
// need to be terminated to allow the delete to succeed.
Expand Down