Skip to content

Commit

Permalink
Fix proactive refresh logic, add some tests (#6544)
Browse files Browse the repository at this point in the history
* Fix proactive refresh logic, add some tests

* Changeset
  • Loading branch information
sam-gc committed Aug 19, 2022
1 parent 47895fe commit bea604e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-eels-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/auth": patch
---

Fix proactive refresh logic in Auth when RTDB/Firestore/Storage are in use
59 changes: 59 additions & 0 deletions packages/auth/src/core/auth/auth_impl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,65 @@ describe('core/auth/auth_impl', () => {
});
});

context('with Proactive Refresh', () => {
let oldUser: UserInternal;

beforeEach(() => {
oldUser = testUser(auth, 'old-user-uid');

for (const u of [user, oldUser]) {
sinon.spy(u, '_startProactiveRefresh');
sinon.spy(u, '_stopProactiveRefresh');
}
});

it('null -> user: does not turn on if not enabled', async () => {
await auth._updateCurrentUser(null);
await auth._updateCurrentUser(user);

expect(user._startProactiveRefresh).not.to.have.been.called;
});

it('null -> user: turns on if enabled', async () => {
await auth._updateCurrentUser(null);
auth._startProactiveRefresh();
await auth._updateCurrentUser(user);

expect(user._startProactiveRefresh).to.have.been.called;
});

it('user -> user: does not turn on if not enabled', async () => {
await auth._updateCurrentUser(oldUser);
await auth._updateCurrentUser(user);

expect(user._startProactiveRefresh).not.to.have.been.called;
});

it('user -> user: turns on if enabled', async () => {
auth._startProactiveRefresh();
await auth._updateCurrentUser(oldUser);
await auth._updateCurrentUser(user);

expect(oldUser._stopProactiveRefresh).to.have.been.called;
expect(user._startProactiveRefresh).to.have.been.called;
});

it('calling start on auth triggers user to start', async () => {
await auth._updateCurrentUser(user);
auth._startProactiveRefresh();

expect(user._startProactiveRefresh).to.have.been.calledOnce;
});

it('calling stop stops the refresh on the current user', async () => {
auth._startProactiveRefresh();
await auth._updateCurrentUser(user);
auth._stopProactiveRefresh();

expect(user._stopProactiveRefresh).to.have.been.called;
});
});

it('onAuthStateChange works for multiple listeners', async () => {
const cb1 = sinon.spy();
const cb2 = sinon.spy();
Expand Down
6 changes: 3 additions & 3 deletions packages/auth/src/core/auth/auth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,9 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
): Promise<void> {
if (this.currentUser && this.currentUser !== user) {
this._currentUser._stopProactiveRefresh();
if (user && this.isProactiveRefreshEnabled) {
user._startProactiveRefresh();
}
}
if (user && this.isProactiveRefreshEnabled) {
user._startProactiveRefresh();
}

this.currentUser = user;
Expand Down

0 comments on commit bea604e

Please sign in to comment.