【问题标题】:Connect user to Firebase from front and back从正面和背面将用户连接到 Firebase
【发布时间】:2017-07-22 10:53:31
【问题描述】:

这是我的情况:

  • 我有一个连接到 Firebase 的前端 (AngularJS),我使用 firebase auth 来验证用户身份。
  • 对于某些特殊操作(例如,有时我需要向用户发送电子邮件),我必须使用 firebase-admin 包调用查询 Firebase 的 Web 服务 (NodeJs)
  • 目前此 Web 服务不受身份验证保护

所以我的问题是如何使用 Firebase 处理前后用户身份验证?

如何获取 Firebase 身份验证 cookie,然后将其发送到我的网络服务以对我的用户进行身份验证?

【问题讨论】:

    标签: angularjs node.js http firebase firebase-authentication


    【解决方案1】:

    Firebase 提供了一些关于如何verify ID tokens 的文档。要在客户端 Angular 代码中检索 ID 令牌,您可以这样做:

    firebase.auth().currentUser.getToken(/* forceRefresh */ true).then(function(idToken) {
      // Send token to your backend via HTTPS
      // ...
    }).catch(function(error) {
      // Handle error
    });
    

    并验证服务器上的 idToken:

    // idToken comes from the client app (shown above)
    admin.auth().verifyIdToken(idToken)
      .then(function(decodedToken) {
        var uid = decodedToken.uid;
        // ...
      }).catch(function(error) {
        // Handle error
      });
    

    【讨论】:

    • 谢谢,这正是我所需要的。我自己会找到的,但是 firebase 文档有点乱
    猜你喜欢
    • 2020-03-04
    • 2016-08-25
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多