Auth: option to report the persisted user immediately and re-validate it in the background

Open
#10,576 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
firebase, typescript

Research direction

Start by reading AuthImpl.initializeCurrentUser and _initializeWithPersistence, including the reload and popup/redirect initialization paths described in the issue. Define how the opt-in setting changes the order of persistence reporting, reload, and resolver startup, while preserving the stated sign-out behavior for non-network failures. Done means the persisted user is reported first and background revalidation updates or clears it as specified.

Written by the indexing model from the issue text.

Description

api: auth type: feature request

Product: Auth (@firebase/auth 1.13.6, firebase 12.11.0), web, installed PWA on Android Chrome.

What happens. On start-up, AuthImpl.initializeCurrentUser calls
reloadAndSetCurrentUserOrClear, which awaits _reloadWithoutSaving (a securetoken refresh when
the stored ID token is over an hour old, then accounts:lookup) before the first
onAuthStateChanged / authStateReady(). On a mobile browser, _initializeWithPersistence also
awaits browserPopupRedirectResolver._initialize first (_shouldInitProactively), which loads the
gapi script and the /__/auth/iframe frame.

On a weak connection (2G/3G, or a signal that neither fails nor succeeds) each of those requests
runs toward its timeout (30 s while navigator.onLine is true). The SDK already keeps the stored
user when the reload fails with auth/network-request-failed, so the wait does not change the
outcome; it only delays it. Because Firestore's FirebaseAuthCredentialsProvider holds its queue
until Auth's first token callback, even cache-only Firestore reads wait for it, so an offline-first
app cannot render cached data until Auth has finished talking to the network.

Measured on a Pixel phone, installed PWA, signed in, weak signal: Auth's first report took
2.4–5.9 s of a 4–9 s start (55–66%). Airplane mode, where every request fails at once, started in
0.4 s.

What we'd like. An opt-in setting (for example initializeAuth(app, { revalidateInBackground: true }))
under which initialisation:

  1. reports the persisted user as soon as persistence is read;
  2. runs the reload after the first report: on success it persists and notifies as reload() does;
    on a non-network failure it signs out (so a disabled or revoked account still ends its
    session, just after the first screen rather than before it);
  3. starts the proactive popup/redirect iframe without awaiting it.

Servers still verify the ID token on every request, so this changes when the client learns that a
session was revoked, not what the session can reach.

What we do meanwhile. A patch-package patch with exactly these changes to
dist/esm/index-*.js, about 30 lines; happy to share it or open a PR if the shape is welcome.

Dominant language
TypeScript
Stars
5.2k
Forks
1k
Avg merge
2d 2h
Merged PRs (30d)
77

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/firebase-js-sdk

All issues in firebase/firebase-js-sdk

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.