I have both a Firebase web app and a Chrome Extension - user will use Chrome Extension to save article that can be read later from their web app.
For web app, I use Firebase Auth. But I am unsure how to pass user token or id to Chrome Extension, so that user does not need to login twice.
I have read this question, but it is not clear to me how to send token to Chrome Extension from web app.
Thanks!
There's a detailed answer with examples in this github post
Short scheme is following:
Details from post:
In a Firebase function (or a custom server) handle the token creation using firebase function createCustomToken
const admin = require('firebase-admin')
const functions = require('firebase-functions')
admin.initializeApp()
module.exports functions.https.onCall(uid =>
admin.auth().createCustomToken(uid)
)
After a successful login, request the server for a custom Token passing the uid of the logged user
firebase.functions().httpsCallable('createToken')(uid)
Then send it to the chrome extension
chrome.runtime.sendMessage(extensionID, token)
Finally in the extension when receiving a message, log the user with firebase using the custom token
firebase.initializeApp(firebaseConfig)
firebase.auth().onAuthStateChanged(function (user) {
console.log(
'User state change detected from the Background script of the Chrome Extension:',
user
)
})
chrome.runtime.onMessageExternal.addListener(
(token, sender, sendResponse) => {
firebase
.auth()
.signInWithCustomToken(token)
.catch((error) => {
console.log('error', error)
})
return true
}
)
Your background script will log the authStateChanged event where you can see that the auth is not isAnonymous anymore and the email attributes is the one of the user logged through your app !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With