Skip to content

[BUG] Silent Hangs and Infinite Load State in Firebase Authentication State Listener (AuthContext.tsx) #686

Description

@just-tanvi

Description

While reviewing the authentication flow, I noticed that the Firestore onSnapshot listener used in AuthContext.tsx does not include an error callback.

Currently, the loading state is only cleared when the snapshot successfully returns data. If Firestore encounters an error during the initial read (for example, due to permission issues, connectivity problems, or missing documents), the success callback never executes and the loading state is never updated.

As a result, users can become stuck on a loading screen indefinitely.

Why This Matters

Authentication is one of the first systems users interact with. If the initial Firestore sync fails and the loading state is never resolved, the application appears frozen even though the underlying issue may be recoverable.

Adding proper error handling would make the authentication flow more resilient and provide a better user experience during unexpected Firestore failures.

Evidence

Location:

src/context/AuthContext.tsx

Around lines 308–342, onSnapshot() is registered without an error callback.

Current pattern:

onSnapshot(
  doc(db, collectionName, docId),
  (docSnapshot) => {
    // success logic
    setIsLoading(false);
  }
);

If the listener fails, setIsLoading(false) is never reached.

Impact

  • Users may become stuck on a loading screen indefinitely.
  • Firestore failures are not handled gracefully.
  • Makes debugging authentication issues more difficult.
  • Creates a poor first-time user experience during network or permission failures.

Suggested Fix

Add an error callback to the Firestore listener and ensure the loading state is always resolved.

Example:

unsubscribeSnapshot.current = onSnapshot(
  doc(db, collectionName, docId),
  (docSnapshot) => {
    // existing success logic
    setIsLoading(false);
  },
  (error) => {
    console.error("Firestore user sync error:", error);
    setIsLoading(false);
  }
);

Optionally, a user-facing error state could be shown in the future, but resolving the loading state should be the minimum requirement.

Acceptance Criteria

  • Add an error callback to the Firestore onSnapshot listener.
  • Ensure isLoading is cleared when snapshot initialization fails.
  • Verify users are not trapped in an infinite loading state.
  • Confirm authentication still behaves correctly under normal conditions.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions