Firebase身份验证(不是一个函数,不是一个构造函数)

4

我不知道出了什么问题。我正在使用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;
  // ...
});

我刚刚按照文档中的步骤进行了操作。


我已经找到了一个解决方法,可以使用客户端的匿名用户身份验证。请详细说明您的用例,这样我就可以根据它编写答案。 - Niwin Santhosh
3个回答

1

1
你的第一个错误可能来自某个拼写错误。
注意它说signInWithLoginAndPassword,函数应该叫做signInWithEmailAndPassword。在发布的代码中,它被正确使用了,所以问题可能出在其他地方。
你没有发布使用此代码的代码,但我认为当你创建provider变量时会发生这个错误,你将其用于firebase.auth().signInWithPopup(provider)
那一行应该是var provider = new firebase.auth.GoogleAuthProvider(); 根据错误消息,我想你可能正在执行new firebase.auth().GoogleAuthProvider();,如果是这样,请省略auth后面的括号。

1
抱歉,但这些错误是我手动写的。实际上,我有“signInEmailAndPassword”,但问题仍然存在。我省略了这些括号,问题仍然存在。 - elzoy
实际的错误信息是什么?如果您能够提供出错信息和相应代码,我可以更好地帮助您。否则,我只能盲目猜测。 - marton
在代码的某一行出现了类型错误:firebase.auth(...).signInWithEmailAndPassword。如果去掉括号,同样会出现这个错误。以下是相关代码:firebase.auth().signInWithEmailAndPassword(req.body.login, req.body.password).catch(function(error) { // 处理错误 var errorCode = error.code; var errorMessage = error.message; // ... }); - elzoy

1

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

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

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

请参阅Firebase文档中的此页面,了解有关设置服务器端进程的详细信息。

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. - maudulus
我不理解为什么你在引用 https://firebase.google.com/docs/admin/setup,因为它在所有调用中都使用了“admin”。 - maudulus
变量名称可以是任何东西,只要在“require”调用和其他调用之间匹配即可。话虽如此:我已将其修改以匹配当前文档。 - Frank van Puffelen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接