Skip to content

Fix: No error handling around onAuthStateChanged listener - #351

Closed
khushboo-khatoon wants to merge 1 commit into
Nitya-003:mainfrom
khushboo-khatoon:fix/auth-listener-error-handling
Closed

Fix: No error handling around onAuthStateChanged listener#351
khushboo-khatoon wants to merge 1 commit into
Nitya-003:mainfrom
khushboo-khatoon:fix/auth-listener-error-handling

Conversation

@khushboo-khatoon

Copy link
Copy Markdown
Contributor

Fixes #343

Description

The onAuthStateChanged listener in the useAuth hook only provided a success callback (onNext). It didn't use the third onError callback that Firebase supports, so if the listener itself hit an internal error (e.g. a token refresh failure), there was no way to handle it.

Since setLoading(false) was only called inside the success callback, an error in the listener could leave loading stuck as true forever — causing the app to appear stuck (e.g. an infinite loading spinner) with no indication of what went wrong.

Changes

  • Added the third onError callback to onAuthStateChanged.
  • On listener error: logs it via console.error, defaults user to null, and sets loading to false so the app never gets stuck in an infinite loading state.

File

frontend/hooks/useAuth.ts

Before

const unsubscribe = onAuthStateChanged(auth, (currentUser) => {
  setUser(currentUser);
  setLoading(false);
});

After

const unsubscribe = onAuthStateChanged(
  auth,
  (currentUser) => {
    setUser(currentUser);
    setLoading(false);
  },
  (error) => {
    console.error('Auth state change error:', error);
    setUser(null);
    setLoading(false);
  }
);

How to Test

  1. Load any page that uses the useAuth hook.
  2. Simulate an internal error in the onAuthStateChanged listener (e.g. temporarily mock the listener to invoke its error callback instead of the success callback).
  3. Confirm loading becomes false and user becomes null, instead of the app hanging indefinitely in a loading state.

Impact

Prevents the app from getting stuck in an infinite loading state if the Firebase Auth listener encounters an internal error — a rare but possible production issue that would otherwise be silent and hard to debug.

Checklist

  • Code builds and runs locally
  • No unrelated code changes
  • No new lint/type errors introduced

@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

@khushboo-khatoon is attempting to deploy a commit to the Nitya Gosain's projects Team on Vercel.

A member of the Team first needs to authorize it.

@khushboo-khatoon

Copy link
Copy Markdown
Contributor Author

hey @Nitya-003 ,

PR is now ready to review and merge .
thank u ( :

@khushboo-khatoon khushboo-khatoon closed this by deleting the head repository Jul 31, 2026
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.

No error handling around onAuthStateChanged listener

1 participant