首页 文章

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

提问于
浏览
3

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

我刚刚完成了文档中的内容 .

3 回答

  • 2

    无法使用电子邮件密码或其中一个社交提供程序将您的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的此页面 .

  • 1

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

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

    let provider = new firebase.auth.GoogleAuthProvider()

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

  • 1

    你的第一个错误可能来自某个地方的错字 .

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

    注意它表示signInWith Login AndPassword,该函数名为signInWith Email AndPassword . 在发布的代码中,'s used correctly, so it'可能在其他地方 .

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

    你没有在你使用它的地方发布代码,但是我假设在你创建 provider 变量时发生了这个错误,你在 firebase.auth().signInWithPopup(provider) 中使用了

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

    基于错误消息,我认为你可能正在做 new firebase.auth().GoogleAuthProvider(); 在auth之后省略括号,如果是这样的话 .

相关问题