Skip to content

Commit 7e14a59

Browse files
authored
Update refresh token buffer from 30 seconds to 60 seconds (#9296)
* Update the token refresh time from 30 seconds to 1 minute and add unit tests * Run yarn run demo
1 parent 870af07 commit 7e14a59

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/auth/src/core/auth/auth_impl.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ describe('core/auth/auth_impl', () => {
317317
let exchangeTokenStub: sinon.SinonStub;
318318
let mockToken: FirebaseToken;
319319
let expiredMockToken: FirebaseToken;
320+
let soonToExpireMockToken: FirebaseToken;
320321
let tokenRefreshHandler: TokenRefreshHandler;
321322
const tokenKey = `firebase:persistence-token:${FAKE_APP.options.apiKey!}:${
322323
FAKE_APP.name
@@ -331,6 +332,10 @@ describe('core/auth/auth_impl', () => {
331332
token: 'test-token',
332333
expirationTime: Date.now() + 300000 // 5 minutes from now
333334
};
335+
soonToExpireMockToken = {
336+
token: 'test-token',
337+
expirationTime: Date.now() + 60000 // 1 minutes from now
338+
};
334339
expiredMockToken = {
335340
token: 'expired-test-token',
336341
expirationTime: Date.now() - 1000 // 1 second ago
@@ -364,6 +369,28 @@ describe('core/auth/auth_impl', () => {
364369
expect(exchangeTokenStub).not.to.have.been.called;
365370
});
366371

372+
it('should refresh the token if token is expiring in next 1 minute and a token refresh handler is set', async () => {
373+
persistenceStub._get
374+
.withArgs(tokenKey)
375+
.resolves(soonToExpireMockToken as any);
376+
auth.setTokenRefreshHandler(tokenRefreshHandler);
377+
378+
exchangeTokenStub.callsFake(async () => {
379+
// When exchangeToken is called, simulate that the new token is persisted.
380+
persistenceStub._get.withArgs(tokenKey).resolves(mockToken as any);
381+
});
382+
383+
const token = await auth.getFirebaseAccessToken();
384+
385+
expect(tokenRefreshHandler.refreshIdpToken).to.have.been.calledOnce;
386+
expect(exchangeTokenStub).to.have.been.calledWith(
387+
auth,
388+
'test-idp',
389+
'new-id-token'
390+
);
391+
expect(token).to.eql('test-token');
392+
});
393+
367394
it('should refresh the token if it is expired and a token refresh handler is set', async () => {
368395
persistenceStub._get.withArgs(tokenKey).resolves(expiredMockToken as any);
369396
auth.setTokenRefreshHandler(tokenRefreshHandler);

packages/auth/src/core/auth/auth_impl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
115115
private redirectUser: UserInternal | null = null;
116116
private isProactiveRefreshEnabled = false;
117117
private readonly EXPECTED_PASSWORD_POLICY_SCHEMA_VERSION: number = 1;
118-
private readonly TOKEN_EXPIRATION_BUFFER = 30_000;
118+
// 1 minute buffer for expiry
119+
// Intended for tokens issued by IdentityPlatform APIs in regionalized Firebase Auth, which can
120+
// have expiry as low as 5 minutes. This should provide enough buffer to the Firebase SDKs for
121+
// token usage before it expires.
122+
private readonly TOKEN_EXPIRATION_BUFFER = 60_000;
119123

120124
// Any network calls will set this to true and prevent subsequent emulator
121125
// initialization

0 commit comments

Comments
 (0)