firebase / firebase/firebase-js-sdk
Don't wait resolving firebase.auth() when enablePersistence() is used
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 37
Description
### Describe the problem
Once signed-in, internally Firestore starts to wait for auth to give its first credential changed event before enabling persistence.
The behaviour remains the same even if `firebase.auth()` is completely removed.
This _verification_ doesn't actually change anything even if the Firestore rules have changed to return 403 on requested resources: cache is returned as-is, every time.
### Steps to reproduce / Relevant Code:
```javascript
firebase.firestore().enablePersistence();
console.log('Fetching data...');
firebase.firestore().doc('foo/bar').onSnapshot(snap => {
console.log('Data source:', snap.metadata.fromCache ? 'cache' : 'server')
console.log(snap.data())
})
// firebase.auth().signInAnonymously(); // Single call is enough
```
```
Fetching data...
‼‼️‼‼️ Waiting for internal call to firebase.auth()...
Data source: cache
► {hotdogStatus: "Not a sandwich"}
Uncaught Error in onSnapshot: FirebaseError: Missing or insufficient permissions.
```
`firebase.auth().signOut()` makes Firestore act **offline-first** again.
The same as removing `IndexedDB > firebaseLocalStorageDb > firebaseLocalStorage > 0`
```
Fetching data...
Data source: cache
► {hotdogStatus: "Not a sandwich"}
Uncaught Error in onSnapshot: FirebaseError: Missing or insufficient permissions.
```
### Expected behavior:
When `enablePersistence()` is used along `auth()`, return cache right away.
It doesn't seem like a security breach. Especially because cache is returned in any case, just with a delay.
I don't feel comfortable using `Local Storage` workarounds to duplicate data:
```javascript
import useLocalStorage from '@rehooks/local-storage';
...
const [data, setData] = useLocalStorage('cachedData', {});
...
firebase.firestore().enablePersistence()
firebase.firestore().doc('foo/bar').onSnapshot(snap => setData(snap.data()) )
firebase.auth();
```
### Describe your environment
* Operating System version: macOS Catalina
* Browser version: Google Chrome 83.0.4103.116
* Firebase SDK version: 7.15.2
* Firebase Product: auth, firestore
Contributor guide
Assessment
This issue has not been assessed yet.