node.js - 我们如何在 node.js 中使用 Firebase Auth 创建用户?

标签 node.js firebase firebase-authentication

我正在尝试在 nodejs 中使用 Firebase 身份验证创建用户帐户。它不起作用,我不知道为什么。这是 nodejs 生成的代码和错误:

var config = {.......}; // i can't share the config

var app = firebase.initializeApp(config);

var db = app.database();
var auth = app.auth();

auth.createUserWithUsernameAndPassword("anemail@gmail.com", "@NewPassWord").then(
    function(user){ console.log(user);},
    function(error){ console.error(error);}
);

NodeJS 产生的错误:

TypeError: Object [object Object] has no method 'createUserWithUsernameAndPassword'
    at Object.<anonymous> (/home/antonio/Projects/firebase/app.js:20:6)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

最佳答案

我终于让它在 Node.js 中工作了。您的 app 是否是 firebase-admin 的实例,而不是 firebase

在我的 express 应用程序中,我在主 app.js 中初始化 firebase,就像这样(非常标准的票价):

var firebase = require("firebase");

firebase.initializeApp
({
  apiKey: "xxx",
  authDomain: "xxx",
  databaseURL: "xxx",
  storageBucket: "xxx",
  messagingSenderId: "xxx"
});

然后在负责创建用户的路由中我这样做:

var firebase = require("firebase");

router.post('/register', function(req, res, next)
{
  var email = req.body.email;
  var password = req.body.password;

  firebase.auth().createUserWithEmailAndPassword
  (
    email,
    password
  )
  .then(function(userRecord)
  {
    res.location('/user/' + userRecord.uid);
    res.status(201).end();
  })
  .catch(function(error)
  {
    res.write
    ({
      code: error.code
    });
    res.status(401).end();
  });
});

关于node.js - 我们如何在 node.js 中使用 Firebase Auth 创建用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485998/

相关文章:

node.js - 如何在保存后将属性保存到 Mongoose 文档

node.js - 可以在没有全局安装的情况下在 Node 模块中包含命令行吗?

javascript - 如何根据 JSON 数组信息的内容附加 div

swift - Firebase 添加子项时,另一个子项被删除

swift - 如何处理 Firebase Auth 错误?/Swift/Firebase

ios - Firebase iOS 恢复密码

javascript - 表达 cookieParser() 困惑

macos - Firebase SDK 2.4.2 中不支持 OSX

android - 是什么导致 "permission denied"- 带有 FIREBASE 和 FLUTTER 的消息

javascript - 如何验证 firebase 用户当前密码