Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: shared auth in chrome extension content script #8784

Open
knotri opened this issue Feb 11, 2025 · 0 comments
Open

FR: shared auth in chrome extension content script #8784

knotri opened this issue Feb 11, 2025 · 0 comments

Comments

@knotri
Copy link

knotri commented Feb 11, 2025

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.

@knotri knotri added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Feb 11, 2025
@jbalidiong jbalidiong added api: auth and removed question new A new issue that hasn't be categoirzed as question, bug or feature request labels Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants