【问题标题】:Firebase authentication (is not a function, is not a constructor)Firebase 身份验证(不是函数,也不是构造函数)
【发布时间】:2016-11-07 02:10:32
【问题描述】:

我不知道出了什么问题。我正在使用 Node.js 并尝试使用电子邮件/密码和 Google 身份验证登录。我已经在 Firebase 控制台中启用了所有这些。

npm Firebase 版本 - 3.1.0

部分代码:

var firebase = require('firebase');

var config = {
  apiKey: "AIzaSyAH27JhfgCQfGmoGTdv_VaGIaX4P-qAs_A",
  authDomain: "pgs-intern.firebaseapp.com",
  databaseURL: "https://pgs-intern.firebaseio.com",
  storageBucket: "pgs-intern.appspot.com",
};

firebase.initializeApp(config);

app.post('/login', function(req, res) {
  var auth = firebase.auth();

  firebase.auth().signInWithEmailAndPassword(req.body.login, req.body.password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // ...
  });
}

错误:firebase.auth(...).signInWithLoginAndPassword 不是函数 要么 错误:我写的时候,firebase.auth(...).GoogleAuthProviders 不是构造函数

firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // ...
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential;
  // ...
});

我只是按照文档中的内容做了。

【问题讨论】:

  • 我找到了一种解决方法,使用来自客户端的匿名用户身份验证。请详细说明您的用例,以便我可以据此编写答案。

标签: node.js firebase firebase-authentication


【解决方案1】:

您的第一个错误可能来自某个地方的拼写错误。

firebase.auth(...).signInWithLoginAndPassword is not a function

注意上面写着signInWithLoginAndPassword,这个函数叫做signInWithEmailAndPassword。在发布的代码中它使用正确,所以它可能在其他地方。

firebase.auth(...).GoogleAuthProviders is not a constructor

您尚未发布使用此代码的代码,但我假设此错误发生在您创建 provider 变量时,您在 firebase.auth().signInWithPopup(provider) 中使用该变量

那行应该是var provider = new firebase.auth.GoogleAuthProvider();

根据错误消息,我认为您可能正在做new firebase.auth().GoogleAuthProvider(); 如果是这种情况,请在 auth 之后省略括号。

【讨论】:

  • 抱歉,我手动编写了这些错误。事实上,我有 signInEmailAndPassword 并且仍然存在问题。我省略了这些括号,问题仍然存在。
  • 那么实际的错误信息是什么?如果您准确发布您遇到的错误以及它的来源代码,我可以提供更好的帮助,否则我会盲目猜测。
  • Type Error: firebase.auth(...).signInWithEmailAndPassword at line where is ``` firebase.auth().signInWithEmailAndPassword(req.body.login, req.body.password).catch(function(error) { // 处理错误here.var errorCode = error.code; var errorMessage = error.message; // ... });``` 没有括号有同样的错误但没有 (...)
【解决方案2】:

不要通过 Auth() 函数调用 GoogleAuthProvider。

根据文档,您必须创建一个 GoogleAuthProvider 实例。

let provider = new firebase.auth.GoogleAuthProvider()

请查看以下链接https://firebase.google.com/docs/auth/web/google-signin

【讨论】:

    【解决方案3】:

    无法使用电子邮件+密码或社交提供商之一将您的 node.js 应用程序登录到 firebase。

    服务器端进程使用所谓的服务帐号登录 Firebase。关键的区别在于您初始化应用程序的方式:

    var admin = require('firebase-admin');
    admin.initializeApp({
      serviceAccount: "path/to/serviceAccountCredentials.json",
      databaseURL: "https://databaseName.firebaseio.com"
    });
    

    请参阅Firebase documentation for details on setting up a server-side process 的此页面。

    【讨论】:

    • The 'serviceAccount' property specified in the first argument to initializeApp() is deprecated and will be removed in the next major version. You should instead use the 'firebase-admin' package. See https://firebase.google.com/docs/admin/setup for details on how to get started.
    • 我不明白您为什么在使用admin 进行所有呼叫时引用firebase.google.com/docs/admin/setup
    • 变量名称可以是任何名称,只要它在require 调用和其他调用之间匹配即可。也就是说:我对其进行了修改以匹配当前的文档。
    【解决方案4】:

    我也通过添加解决了这个错误 auth 要求。

    const firebase = require("firebase/app");
    require("firebase/auth");
    ...
    var provider = new firebase.auth.GoogleAuthProvider();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2020-03-30
      • 2022-08-20
      • 2018-07-11
      • 2021-11-14
      • 2020-02-19
      相关资源
      最近更新 更多