firebase / firebase/firebase-js-sdk

Auth: signInWithCustomToken throws "Database is closing/hidden" during rapid UI changes (Not fixed by 12.18.0)

Open
#10,333 3 comments 2 reactions 0 assignees View on GitHub
api: auth question Repro Needed stack:Angular testing-sdk
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

Windows 11

### Environment (if applicable)

Angular 22 Application

### Firebase SDK Version

12.18.0

### Firebase SDK Product(s)

Auth

### Project Tooling

Angular CLI, pnpm, Firebase Local Emulator Suite.

### Detailed Problem Description

When calling signInWithCustomToken immediately after rapid component state changes or navigation in a Single Page Application, Firebase occasionally throws an IndexedDB error: code: undefined message: Database is closing/hidden

We noticed that issue #10318 and PR #10325 recently introduced a fix for this exact error message in 12.18.0. However, that fix appears to have only targeted signInWithPopup and background tab visibility. We are still consistently hitting this exact same race condition when using signInWithCustomToken during rapid UI changes.

If signInWithCustomToken fails here, the one-time custom token is consumed on the backend but the client session fails to persist. This permanently breaks the authentication session for our users unless we implement an aggressive retry loop to catch this specific transient database error.

Similar to #10318, the thrown error has code: undefined, which makes graceful fallback difficult without string matching the message.

### Steps and code to reproduce issue

The bug is a race condition, so it happens intermittently based on component lifecycle timing.

Generate a custom token via a backend Cloud Function.
In a frontend SPA (e.g., Angular), trigger a rapid route navigation or heavy component destruction/initialization.
Immediately call signInWithCustomToken(auth, customToken).
Observe the console for the Database is closing/hidden error.
To work around this, we currently have to catch the exact error message string and retry the sign-in with a 500ms delay, which allows the IndexedDB connection to stabilize and the sign-in to succeed.

typescript

try {
await signInWithCustomToken(auth, customToken);
} catch (error) {
// Catch the unclassified database error
const isDbClosing = (error as Error)?.message?.includes('Database is closing/hidden');

if (isDbClosing) {
// Wait for the IndexedDB connection to stabilize
await new Promise(resolve => setTimeout(resolve, 500));

// Retrying succeeds
await signInWithCustomToken(auth, customToken);
} else {
throw error;
}
}

Contributor guide

Open the contributing guide

Research direction

Start by reviewing issue #10318 and PR #10325, then trace the signInWithCustomToken flow and its session persistence during rapid component navigation. Reproduce the intermittent error in the Angular SPA scenario with the Firebase Local Emulator Suite; done means custom-token sign-in persists successfully without the Database is closing/hidden failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
authentication, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.