Skip to content

FR: shared auth in chrome extension content scriptΒ #8784

Open
@knotri

Description

@knotri

Operating System

mac OS

Environment (if applicable)

Chrome 115

Firebase SDK Version

11.1.0

Firebase SDK Product(s)

Auth

Project Tooling

React, chrome extention

Detailed Problem Description

Using this guide I can authorize user in chrome extension background script
https://developer.chrome.com/docs/extensions/how-to/integrate/oauth

However, I don't know how to share/transfer this authorized state to chrome extension content script (which exists inside context of 3rd party website)

I also read this article https://firebase.google.com/docs/auth/web/auth-state-persistence
It looks like, creating my own persistence which uses chrome.storage (which exists both in background and content script) should work.

class ChromeStorageAsFirebase {
    protected constructor (
      readonly type: 'Local'
    ) {}

    _isAvailable (): Promise<boolean> {
        return Promise.resolve(true)
    }

    _set (key: string, value: any): Promise<void> {
      return chrome.storage.local.set({ [key]: JSON.stringify(value) })
    }

    async _get<T extends any> (key: string) {
      const r = await chrome.storage.local.get(key)
      return JSON.parse(r[key])
    }

    async _remove (key: string): Promise<void> {
        return await chrome.storage.local.remove(key)
    }
  }

  const auth = getAuth(app) // Will use indexedDB, so no user info :(
  setPersistence(auth, ChromeStorageAsFirebase as any) // will work correctly to save info to chrome.storage, but its too late for get user info

However, using my own persistence instance I can save data in background script.
// after user login, from background scripts I see data correctly saved to chrome storage
But then, in content script, I first must to initialize firebase auth const auth = getAuth() and after setting custom persistence, I cannot find a way to set persistence "before init", so firebase auth code initially checks auth state object in default persistence (which is indexedDB) and then remembers it. And setting custom persistence later doesn't do much.

persistence: [indexedDBLocalPersistence]

It would be nice if I could pass my own persistence here, instead of default indexedDB

Steps and code to reproduce issue

Not sure there are some code to repro.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions