Skip to content

fix(fxa-settings): Handle missing account data in localStorage#20127

Open
vbudhram wants to merge 1 commit intomainfrom
fxa-13170
Open

fix(fxa-settings): Handle missing account data in localStorage#20127
vbudhram wants to merge 1 commit intomainfrom
fxa-13170

Conversation

@vbudhram
Copy link
Contributor

@vbudhram vbudhram commented Mar 2, 2026

Because

  • There could be a race condition in iOS when opening settings (the localStorage might not have the account yet)

This pull request

  • Guard account.primaryEmail and hasPassword access in Settings
  • recover currentAccountUid via URL params or accounts storage scan when missing, and add Sentry diagnostics for future triage.

Issue that this pull request solves

Closes: https://mozilla-hub.atlassian.net/browse/FXA-13170

Checklist

  • My commit is GPG signed.
  • If applicable, I have modified or added tests which pass locally.
  • I have added necessary documentation (if appropriate).
  • I have verified that my changes render correctly in RTL (if appropriate).

Screenshots (Optional)

Please attach the screenshots of the changes made in case of change in user interface.

Other information (Optional)

  1. Start FxA locally (yarn start or yarn start mza)
  2. Sign in and navigate to http://localhost:3000/settings
  3. Open browser DevTools (Console tab)

Test 1: Missing currentAccountUid (recovery path)

Simulates iOS WKWebView evicting the UID key while account data remains.

  1. Delete the UID key:
    localStorage.removeItem('__fxa_storage.currentAccountUid');
  2. Refresh the page

Expected: Settings page loads normally. findActiveAccountUid() recovers the UID from the accounts map and restores it. You can verify recovery by checking:

JSON.parse(localStorage.getItem('__fxa_storage.currentAccountUid'));

Test 2: Full storage eviction (redirect path)

Simulates complete localStorage loss where no account data can be recovered.

  1. Delete both keys:
    localStorage.removeItem('__fxa_storage.currentAccountUid');
    localStorage.removeItem('__fxa_storage.accounts');
  2. Refresh the page

Expected: Graceful redirect to / (sign-in). Console shows:

Account data unavailable, redirecting to sign-in

Before the fix: Page crashes with an unhandled error: Account data not loaded from localStorage.

Test 3: Unit tests

nx test-unit fxa-settings -- --testPathPattern="Settings/index.test" -t "redirects to root when account data is unavailable"

…3170)

Guard account.primaryEmail and hasPassword access in Settings, recover
currentAccountUid via URL params or accounts storage scan when missing,
and add Sentry diagnostics for future triage.
@vbudhram vbudhram marked this pull request as ready for review March 2, 2026 19:53
@vbudhram vbudhram requested a review from a team as a code owner March 2, 2026 19:53
@vbudhram vbudhram self-assigned this Mar 2, 2026
}

if (!uid) {
Sentry.captureMessage('setAccountData called with no currentAccountUid', {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some sentry logging here to see how often this happens.

await waitFor(() => {
expect(mockNavigateWithQuery).toHaveBeenCalledWith('/');
});
warnSpy.mockRestore();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to assert that warn was called?

* Used as a recovery path when WKWebView storage eviction loses the UID
* but account data still exists in the accounts map.
*/
export function findActiveAccountUid(): string | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call it findLastActiveAccountUid()? I feel like that more accurately describes the routine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants