我正在尝试在 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/