firebase auth 不是一个函数()

firebase auth is not a function()

我正在按照以下 github 在 node.js、https://github.com/hvalls/node-firebase-auth-example

上测试 firebase 的身份验证

我得到 firebase.auth() 不是函数。

我试过以下方法:

  1. 删除 Firebase-admin
  2. 删除 Firebase
  3. 通过 Npm 重新安装 Firebase

我就是想不通怎么了。

user_service.js

const firebaseapp = require("firebase/app");
require("firebase/auth");

const apiKey = xxxx
const fb = firebaseapp.initializeApp({
  apiKey: apiKey,
});

exports.addUser = (email, password) =>
  fb.auth().createUserWithEmailAndPassword(email, password);

exports.authenticate = (email, password) =>{
    
    fb.auth().signInWithEmailAndPassword(email, password);
}

Route.js

router.post('/now', function (req, res, next){
  console.log('Here');
  try {
    const u =  userService.authenticate(req.body.email, req.body.password);
  } catch (error) {
    console.log("Error : " + error);
    req.sendStatus(500).send(error);  
  }
});

您最好使用不使用 name-spaced 语法的 Admin SDK on server. The error you are getting might be because you've installed Modular SDK (v9.0.0+)。尝试安装 Admin SDK (firebase-admin) 并重构代码,如下所示:

router.post('/user', async function (req, res, next){
  try {
    const newUser = await userService.addUser(req.body.email, req.body.password);
    return res.json(newUser)
  } catch (error) {
    console.log("Error : " + error);
    req.sendStatus(500).send(error);  
  }
});
const admin = require("firebase-admin");

admin.initializeApp({
  // service account
});

exports.addUser = async (email, password) => {
  return admin.auth().createUser({ email, password });
}

您无法使用 Admin SDK 登录用户(除非您使用的是基于会话的身份验证),因此在创建用户后,您应该使用客户端 SDK 在您的客户端应用程序上使用 signInWithEmailAndPassword() 登录用户。