简体   繁体   English

Chrome 扩展程序如何从 Firebase web 应用程序获取用户凭据而无需用户登录两次?

[英]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:简短方案如下:

  1. Create cloud function that generates custom firebase token创建生成自定义 firebase 令牌的云 function
  2. In web app upon login request a custom token from the cloud function在 web 应用程序中,登录后从云端请求自定义令牌 function
  3. Using chrome API send message to extension使用 chrome API 向分机发送消息
  4. In extension on message use token to log into firebase在消息的扩展中使用令牌登录到 firebase
  5. In extension listen to firebase auth state change to log out在extension listen firebase auth state更改注销

Details from post:帖子的详细信息:

Firebase function / Auth Server Firebase function / 认证服务器

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)
)

Webapp网络应用

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)

Extension延期

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.

相关问题 如何在没有 firebase 身份验证的情况下获取用户 ID? - how to get the user ID without firebase authentication? 如何在Flutter移动应用程序中将Azure AD用户登录到Firebase? - How to sign a Azure AD user into Firebase in a Flutter mobile app? 如何从 Android 应用程序中删除 Firebase 用户? - How to delete a Firebase user from Android App? 如何防止用户删除Firebase App - How to prevent User from deleting Firebase App 如何使用 Firebase 身份验证删除多设备登录的单用户凭据 - How To Delete Single user Credential with multiple device login with Firebase Authentication Google Firebase 注销并忘记 Android 应用中的用户 - Google Firebase sign out and forget user in Android app 如何使用 Firebase 身份验证保持用户登录 Flutter web 应用程序 - How to keep a user logged in Flutter web app using Firebase Authentication Chrome 扩展程序中的 Firebase 谷歌身份验证 - 获取凭据时出错 - Firebase google authentication in Chrome Extension - Error getting credential 如何在没有用户登录的情况下检索 .NET Core Web API 的 MS Graph 访问令牌 - How to retrieve an MS Graph access token for a .NET Core Web API without user sign in Firebase auth - 从网站中的应用程序登录用户 - Firebase auth - login user from app in website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM