Skip to content

Commit

Permalink
Do not invoke the App Check token listener for the same token string. (
Browse files Browse the repository at this point in the history
…#5993)

* Do not invoke the App Check token listener for the same token string.

* Fix formatting.

* Address comments.

* Create gold-hounds-begin.md

* Use fewer bytes for log messages.
  • Loading branch information
ehsannas authored Feb 12, 2022
1 parent 50de44c commit bb8f37c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-hounds-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixed a bug that caused Firestore streams to get restarted with the same App Check token.
14 changes: 11 additions & 3 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,9 @@ export class FirebaseAppCheckTokenProvider
* we can unregister it.
*/
private tokenListener!: AppCheckTokenListener;

private forceRefresh = false;

private appCheck: FirebaseAppCheckInternal | null = null;
private latestAppCheckToken: string | null = null;

constructor(
private appCheckProvider: Provider<AppCheckInternalComponentName>
Expand All @@ -482,7 +481,15 @@ export class FirebaseAppCheckTokenProvider
`Error getting App Check token; using placeholder token instead. Error: ${tokenResult.error.message}`
);
}
return changeListener(tokenResult.token);
const tokenUpdated = tokenResult.token !== this.latestAppCheckToken;
this.latestAppCheckToken = tokenResult.token;
logDebug(
'FirebaseAppCheckTokenProvider',
`Received ${tokenUpdated ? 'new' : 'existing'} token.`
);
return tokenUpdated
? changeListener(tokenResult.token)
: Promise.resolve();
};

this.tokenListener = (tokenResult: AppCheckTokenResult) => {
Expand Down Expand Up @@ -534,6 +541,7 @@ export class FirebaseAppCheckTokenProvider
typeof tokenResult.token === 'string',
'Invalid tokenResult returned from getToken():' + tokenResult
);
this.latestAppCheckToken = tokenResult.token;
return new AppCheckToken(tokenResult.token);
} else {
return null;
Expand Down

0 comments on commit bb8f37c

Please sign in to comment.