Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: returns existing promise to a token if one exists instead of a n…
…ew token.
  • Loading branch information
jonathanedey committed Jul 29, 2024
commit 6ee6204bc4fb840f0c5ee1734183daa1b39e4574
16 changes: 11 additions & 5 deletions src/app/firebase-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,29 @@ export interface FirebaseAccessToken {
*/
export class FirebaseAppInternals {
private cachedToken_: FirebaseAccessToken;
private promiseToCachedToken_: Promise<FirebaseAccessToken>;
private tokenListeners_: Array<(token: string) => void>;
private isRefreshing: boolean;

// eslint-disable-next-line @typescript-eslint/naming-convention
constructor(private credential_: Credential) {
this.tokenListeners_ = [];
this.isRefreshing = false;
}

public getToken(forceRefresh = false): Promise<FirebaseAccessToken> {
if (forceRefresh || this.shouldRefresh()) {
return this.refreshToken();
this.promiseToCachedToken_ = this.refreshToken();
}

return Promise.resolve(this.cachedToken_);
return this.promiseToCachedToken_
}

public getCachedToken(): FirebaseAccessToken | null {
return this.cachedToken_ || null;
}

private refreshToken(): Promise<FirebaseAccessToken> {
this.isRefreshing = true;
return Promise.resolve(this.credential_.getAccessToken())
.then((result) => {
// Since the developer can provide the credential implementation, we want to weakly verify
Expand Down Expand Up @@ -108,11 +111,14 @@ export class FirebaseAppInternals {
}

throw new FirebaseAppError(AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
});
})
.finally(() => {
this.isRefreshing = false;
})
}

private shouldRefresh(): boolean {
return !this.cachedToken_ || (this.cachedToken_.expirationTime - Date.now()) <= TOKEN_EXPIRY_THRESHOLD_MILLIS;
return (!this.cachedToken_ || (this.cachedToken_.expirationTime - Date.now()) <= TOKEN_EXPIRY_THRESHOLD_MILLIS) && !this.isRefreshing;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/integration/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('admin.messaging', () => {
});
});

xit('sendToDeviceGroup() returns a response with success count', () => {
it('sendToDeviceGroup() returns a response with success count', () => {
return getMessaging().sendToDeviceGroup(notificationKey, payload, options)
.then((response) => {
expect(typeof response.successCount).to.equal('number');
Expand Down
10 changes: 10 additions & 0 deletions test/unit/app/firebase-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ describe('FirebaseApp', () => {
});
});

it('only refreshes the token once for concurrent calls', () => {
const promise1 = mockApp.INTERNAL.getToken();
const promise2 = mockApp.INTERNAL.getToken();
expect(getTokenStub).to.have.been.calledOnce;
return Promise.all([promise1, promise2]).then((tokens) => {
expect(tokens[0]).to.equal(tokens[1]);
expect(getTokenStub).to.have.been.calledOnce;
})
});

it('Includes the original error in exception', () => {
getTokenStub.restore();
const mockError = new FirebaseAppError(
Expand Down