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

2023-12-20

我正在尝试在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("[email protected] /cdn-cgi/l/email-protection", "@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?

In my express我初始化的应用程序firebase主要在app.js像这样(相当标准的票价):

var firebase = require("firebase");

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

然后在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();
  });
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我们如何在node.js中使用Firebase Auth创建用户? 的相关文章

随机推荐