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

Ant*_*ise 7 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);}
);
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Hay*_*yun 5

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

在我的应用程序中,我主要像这样express初始化(非常标准的费用):firebaseapp.js

var firebase = require("firebase");

firebase.initializeApp
({
  apiKey: "xxx",
  authDomain: "xxx",
  databaseURL: "xxx",
  storageBucket: "xxx",
  messagingSenderId: "xxx"
});
Run Code Online (Sandbox Code Playgroud)

然后在route负责创建用户中我这样做:

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();
  });
});
Run Code Online (Sandbox Code Playgroud)


Cyr*_*Zei 3

嗯,我不认为这个方法"createUserWithUsernameAndPassword"有效,

用这个代替createUserWithEmailAndPassword

当 firebase 使用电子邮件时,您使用了用户名