fix: ensure isAuthenticated$ has fresh value
Description
This PR addresses a race condition in the isAuthenticated$ observable that can occur during the login and logout flows. The issue manifests when the application navigates before the isAuthenticated$ observable has a fresh value, potentially leading to stale authentication states.
Fixes #668
The changes include:
- Introduction of a
RefreshStateenum to track the state of auth state refreshes - Modification of
handleRedirectCallbackto wait for the refresh operation to complete before navigation - Updates to the
logoutfunction to ensure it waits for the refresh operation to complete - Addition of a
refreshComplete$observable to signal when refresh operations are complete
These changes ensure that navigation only occurs after the authentication state has been properly updated, preventing race conditions and stale values in the isAuthenticated$ observable.
References
This PR addresses the issue described in DEV-2553, where users were experiencing blank screens during the authentication flow due to race conditions in the isAuthenticated$ observable.
Testing
The changes can be tested by:
-
Testing the login flow:
- Call
loginWithRedirect() - After redirect, verify that navigation only occurs after
isAuthenticated$has a fresh value - Verify no blank screens appear during the transition
- Call
-
Testing the logout flow:
- Call
logout() - Verify that the operation completes only after the refresh operation is done
- Verify no race conditions occur during the transition
- Call
Environment:
-
Angular 13
-
Node.js 20.15.0
-
macOS 24.4.0
-
[x] This change adds test coverage for new/changed/fixed functionality
Checklist
- [] I have added documentation for new/changed functionality in this PR or in auth0.com/docs
- [] All active GitHub checks for tests, formatting, and security are passing
- [x] The correct base branch is being used, if not the default branch