【问题标题】:Send Firebase auth token to nodejs server将 Firebase 身份验证令牌发送到 nodejs 服务器
【发布时间】:2019-11-13 13:12:55
【问题描述】:

我想让客户端通过前端使用 firebase 登录。稍后我想发送验证他已登录到我的服务器的令牌,以便我可以检查数据库中是否有关于他的一些信息。例如:如果他支付了特定课程的费用。

firebase 文档没有提供有关如何将其发送到我自己的服务器的实际代码:

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

所以,我正在寻找一个想法,或者最好是一些关于如何随每个请求将令牌发送到服务器的代码。

【问题讨论】:

    标签: javascript node.js firebase firebase-authentication http-headers


    【解决方案1】:

    有很多方法可以做到这一点。我喜欢用Axios https://github.com/axios/axios

    axios.post('/your-endpoint', {
        token: your-token
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
    

    【讨论】:

    • 但是如何将令牌放在每个请求上?还是我不必这样做?
    • 我可能需要查看更多代码才能完全理解您的问题。为什么不能只将令牌添加到服务器的每个请求中?
    • 我认为你设法通过会话/cookie 做到了这一点...你认为有更好的方法吗?
    猜你喜欢
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2019-12-01
    • 2013-08-19
    • 2017-06-26
    • 2021-01-29
    • 1970-01-01
    • 2019-08-31
    相关资源
    最近更新 更多