【问题标题】:What is the import statement for the firebase object?firebase 对象的导入语句是什么?
【发布时间】:2020-05-26 15:05:31
【问题描述】:

我希望允许匿名用户登录 Firebase 函数。
像这样:

firebase.auth().signInAnonymously().catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

发件人:https://firebase.google.com/docs/auth/web/anonymous-auth

问题是,我不知道firebase 对象是什么。

我应该使用什么导入语句来获取这个firebase 对象?


我尝试了以下方法:

const firebase = require('firebase-app');

const firebase = require('firebase');

const firebase = require('firebase-admin');

似乎没有 auth().signInAnonymously() 属性。


我在函数日志中收到类似TypeError: firebase.auth is not a functionadmin.auth(...).onAuthStateChanged is not a function 的错误。


这是我要调用的函数:https://firebase.google.cn/docs/reference/js/firebase.auth.Auth#signinanonymously

这是我要导入的 firebase 对象:https://firebase.google.cn/docs/reference/js/firebase

【问题讨论】:

  • 或者我在尝试进行 firebase 部署时收到类似 Error: Cannot find the firebase namespace; be sure to include firebase-app.js before this library. 的消息
  • 你能显示更多代码吗? auth 函数应该存在于const firebase = require('firebase-admin');
  • 你在哪里运行这段代码?如果您使用的是 Node.js,请edit 您的问题并添加适当的标签node.js。如果您在浏览器中运行,请在收到错误时告诉我们您运行的是什么浏览器。

标签: javascript node.js firebase firebase-authentication google-cloud-functions


【解决方案1】:

后端服务(例如 Cloud Functions)不用于登录用户。登录总是在前端进行,用户的 ID 令牌在前端进行管理。如果您需要让后端知道用户的某些 HTTP 请求,您应该将 ID 令牌从客户端传递到服务器,然后是 verify the ID token with the Firebase Admin SDK

Cloud Functions 服务器实例是瞬态的,不会正确和持久地保存登录令牌,这就是您需要在每次请求时从客户端传递它们的原因。

我建议查看您正在使用的前端平台的 Firebase 身份验证文档,以便开始在那里实现登录。

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 2011-11-27
    • 2011-09-22
    • 2017-03-19
    • 2014-02-08
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多