[英]How does Chrome Extension get user credential from Firebase web app without user having to sign in twice?
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.我有一个 Firebase web 应用程序和一个 Chrome 扩展程序 - 用户将使用 Chrome 扩展程序来保存文章,稍后可以从他们的 web 应用程序中阅读。
For web app, I use Firebase Auth.对于 web 应用程序,我使用 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.但我不确定如何将用户令牌或 ID 传递给 Chrome 扩展程序,这样用户就不需要登录两次。
I have read this question , but it is not clear to me how to send token to Chrome Extension from web app.我读过这个问题,但我不清楚如何从 web 应用程序向 Chrome 扩展程序发送令牌。
Thanks!谢谢!
There's a detailed answer with examples in this github post在这个 github 帖子中有一个带有示例的详细答案
Short scheme is following:简短方案如下:
Details from post:帖子的详细信息:
In a Firebase function (or a custom server) handle the token creation using firebase function createCustomToken
在 Firebase function(或自定义服务器)中使用 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登录成功后,向服务器请求自定义令牌,传递登录用户的 uid
firebase.functions().httpsCallable('createToken')(uid)
Then send it to the chrome extension然后发送到chrome扩展
chrome.runtime.sendMessage(extensionID, token)
Finally in the extension when receiving a message, log the user with firebase using the custom token最后在收到消息时的扩展中,使用自定义令牌将用户记录为 firebase
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 !您的后台脚本将记录 authStateChanged 事件,您可以在其中看到 auth 不再是 isAnonymous 并且 email 属性是通过您的应用程序登录的用户之一!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.