Skip to content

Commit c90f54b

Browse files
committed
Update the token refresh time from 30 seconds to 1 minute and add unit tests
1 parent 870af07 commit c90f54b

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
@@ -384,6 +389,28 @@ describe('core/auth/auth_impl', () => {
384389
expect(token).to.eql('test-token');
385390
});
386391

392+
it('should refresh the token if token is expiring in next 1 minute and a token refresh handler is set', async () => {
393+
persistenceStub._get
394+
.withArgs(tokenKey)
395+
.resolves(soonToExpireMockToken as any);
396+
auth.setTokenRefreshHandler(tokenRefreshHandler);
397+
398+
exchangeTokenStub.callsFake(async () => {
399+
// When exchangeToken is called, simulate that the new token is persisted.
400+
persistenceStub._get.withArgs(tokenKey).resolves(mockToken as any);
401+
});
402+
403+
const token = await auth.getFirebaseAccessToken();
404+
405+
expect(tokenRefreshHandler.refreshIdpToken).to.have.been.calledOnce;
406+
expect(exchangeTokenStub).to.have.been.calledWith(
407+
auth,
408+
'test-idp',
409+
'new-id-token'
410+
);
411+
expect(token).to.eql('test-token');
412+
});
413+
387414
it('should force refresh the token when forceRefresh is true', async () => {
388415
persistenceStub._get.withArgs(tokenKey).resolves(mockToken as any);
389416
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)