Firebase auth in background script

I have followed the with-firebase-auth example correctly. Once I sign in in the popup, it seems like auth is persisted to the options page as well, which is great. However, in the background script I just can't seem to get the auth working. It works for making unauthed Firestore requests though. This is how I initialize the background script: const app = initializeApp(FIREBASE_CONFIG); const db = getFirestore(app); const auth = getAuth(app); setPersistence(auth, browserLocalPersistence); Do you guys have a clue on how to do this?
3 Replies
lab
lab13mo ago
You will need to send an auth token down to bgsw
sujantkumarkv
sujantkumarkv13mo ago
hey @webdev can you explain more on how you got the with-firebase-auth working, like my doubt is regarding the flow, googling & chatgpt says we shalln't use firebase logic in the background service worker (bgsw) file & chrome.identity... works in bgsw but not elsewhere & doesn't have access to react state. I'm getting this error (includes my console.logs) Sending message to background to start auth {type: 'startAuth'} message: "OAuth2 not granted or revoked."[[Prototype]]: Object Could not get token in the service worker autTokenError, didn't receive token {message: 'OAuth2 not granted or revoked.'}
webdev
webdev13mo ago
I didn't manage to make it work for now. The idea would be to share the auth token (from user.getToken() in client side) down to bgsw and make authenticated requests directly to firebase services (just like in fetch you would set authentication headers). However, firebase docs are so convoluted that I can't really find a way to do custom requests without setting auth from firebase. If someone manages to find out that would be great