Skip to content

Commit bea604e

Browse files
authored
Fix proactive refresh logic, add some tests (#6544)
* Fix proactive refresh logic, add some tests * Changeset
1 parent 47895fe commit bea604e

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

.changeset/happy-eels-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/auth": patch
3+
---
4+
5+
Fix proactive refresh logic in Auth when RTDB/Firestore/Storage are in use

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,65 @@ describe('core/auth/auth_impl', () => {
306306
});
307307
});
308308

309+
context('with Proactive Refresh', () => {
310+
let oldUser: UserInternal;
311+
312+
beforeEach(() => {
313+
oldUser = testUser(auth, 'old-user-uid');
314+
315+
for (const u of [user, oldUser]) {
316+
sinon.spy(u, '_startProactiveRefresh');
317+
sinon.spy(u, '_stopProactiveRefresh');
318+
}
319+
});
320+
321+
it('null -> user: does not turn on if not enabled', async () => {
322+
await auth._updateCurrentUser(null);
323+
await auth._updateCurrentUser(user);
324+
325+
expect(user._startProactiveRefresh).not.to.have.been.called;
326+
});
327+
328+
it('null -> user: turns on if enabled', async () => {
329+
await auth._updateCurrentUser(null);
330+
auth._startProactiveRefresh();
331+
await auth._updateCurrentUser(user);
332+
333+
expect(user._startProactiveRefresh).to.have.been.called;
334+
});
335+
336+
it('user -> user: does not turn on if not enabled', async () => {
337+
await auth._updateCurrentUser(oldUser);
338+
await auth._updateCurrentUser(user);
339+
340+
expect(user._startProactiveRefresh).not.to.have.been.called;
341+
});
342+
343+
it('user -> user: turns on if enabled', async () => {
344+
auth._startProactiveRefresh();
345+
await auth._updateCurrentUser(oldUser);
346+
await auth._updateCurrentUser(user);
347+
348+
expect(oldUser._stopProactiveRefresh).to.have.been.called;
349+
expect(user._startProactiveRefresh).to.have.been.called;
350+
});
351+
352+
it('calling start on auth triggers user to start', async () => {
353+
await auth._updateCurrentUser(user);
354+
auth._startProactiveRefresh();
355+
356+
expect(user._startProactiveRefresh).to.have.been.calledOnce;
357+
});
358+
359+
it('calling stop stops the refresh on the current user', async () => {
360+
auth._startProactiveRefresh();
361+
await auth._updateCurrentUser(user);
362+
auth._stopProactiveRefresh();
363+
364+
expect(user._stopProactiveRefresh).to.have.been.called;
365+
});
366+
});
367+
309368
it('onAuthStateChange works for multiple listeners', async () => {
310369
const cb1 = sinon.spy();
311370
const cb2 = sinon.spy();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
581581
): Promise<void> {
582582
if (this.currentUser && this.currentUser !== user) {
583583
this._currentUser._stopProactiveRefresh();
584-
if (user && this.isProactiveRefreshEnabled) {
585-
user._startProactiveRefresh();
586-
}
584+
}
585+
if (user && this.isProactiveRefreshEnabled) {
586+
user._startProactiveRefresh();
587587
}
588588

589589
this.currentUser = user;

0 commit comments

Comments
 (0)