Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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