Skip to content
Advertisement

Firebase: authenticated and guest clients connecting to same Firestore Database

My use case is, I have a web app in which I listen to Firestore collections. One of the collections are for public users where anonymous auth is enough. And the second collection is for logged in users, so I implemented custom token auth following this. https://firebase.google.com/docs/auth/web/custom-auth. Once a user logs in, we start a Web Worker to initialise app and listen to Firestore.

My problem is, when a user logs in, the second subscription, the authenticated one, works, but if I try to go to public page, where I don’t need the auth token, from the same tab, I get this error.

JavaScript

Both subscription uses the same firebase app, same configs.

More Info:

these are the rules

JavaScript

And this is the init codes

Guest page:

JavaScript

Logged in page:

JavaScript

Advertisement

Answer

I figured it out. I connect to the same firebase app from two places. First one from a Web Worker, with custom token auth, second one from a different tab from the same browser with anonymous sign in. When the second connection try to initiate a connection with anonymous auth, it doesn’t work because the app is already initiated with custom tokens, which fails this part of the rule,

JavaScript

request.auth.uid is of the one who’s logged in.

Fix:

I had to create another app and a set of configurations to be used for logged in users. and then, I had to give an app name when I initialising and authorising the app.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement