firebase / firebase/firebase-js-sdk

FR: shared auth in chrome extension content script

Open
#8,784 0 comments 1 reaction 0 assignees View on GitHub
api: auth type: feature request
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### 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 {
return Promise.resolve(true)
}

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

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

async _remove (key: string): Promise {
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.

https://github.com/firebase/firebase-js-sdk/blob/45d427494491dfae8f9298c817333c7eb44aae15/packages/auth/index.web-extension.ts#L50

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.