如何使用Node.js在Firebase中注册用户?
问题:
0)用户在Firebase中的身份validation系统中创build(我在Auth选项卡中看到它),
1)但是没有改变数据库。
2)页面似乎被无限加载。
3)仅将“开始1 …”logging到控制台。
码:
router.post('/register', function(req, res, next) { var username = req.body.username; var email = req.body.email; var password = req.body.password; var password2 = req.body.password2; // Validation req.checkBody('username', 'Username is required').notEmpty(); req.checkBody('email', 'Email is required').notEmpty(); req.checkBody('email', 'Email is not valid').isEmail(); req.checkBody('password', 'Password is required').notEmpty(); req.checkBody('password2', 'Passwords do not match').equals(req.body.password); var errors = req.validationErrors(); if(errors){ res.render('users/register', { errors: errors }); } else { console.log("Started 1..."); firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error, userData) { console.log("Started 2..."); if(error){ var errorCode = error.code; var errorMessage = error.message; req.flash('error_msg', 'Registration Failed. Make sure all fields are properly filled.' + error.message); res.redirect('/users/register'); console.log("Error creating user: ", error); } else { console.log("Successfully created"); console.log("Successfully created user with uid:", userData.uid); var user = { uid: userData.uid, email: email, username: username } var userRef = firebase.database().ref('users/'); userRef.push().set(user); req.flash('success_msg', 'You are now registered and can login'); res.redirect('/users/login'); } }); } });
编辑1:
这似乎正在发生:
用户在auth系统中创build。 页面加载大约5分钟(很长!),然后告诉我注册失败,因为电子邮件地址已被使用(不是)。
看起来用户是创build的,但是注册失败,因为代码在创build用户之后循环回来,就好像它没有被创build一样,因此创build了types的错误:这个电子邮件地址已经在我们的数据库中。
但为什么会发生?
不知道这是全部,但这是我想要发生的事情:
用户被创build,但是你没有正确处理它。 没有处理成功案例(履行承诺),只有被拒绝的一个。 另外,当您在发生错误时尝试写入数据库时,这意味着用户没有通过身份validation,这意味着如果您没有更改您的Firebase安全规则,则无法写入。 (默认的Firebase安全规则可防止未经身份validation的用户读取/写入数据库)
这一行:
firebase.auth().createUserWithEmailAndPassword(email,password) .catch(function(error, userData) { ...
应该改成这样:
firebase.auth().createUserWithEmailAndPassword(email, password) .then(userData => { // success - do stuff with userData }) .catch(error => { // do stuff with error })
请注意,发生错误时,您将无法访问userData
,只是出现错误。
希望这可以帮助!
- 如何防止无特权访问rest的APIurl?
- res.json不发送json响应
- 为everyauthconfigurationfindByUserId – 节点,express,mongoose,everyauth
- 安装Express时出错
- 使用req.body通过Mongoose更新和/或添加数组元素属性?
- 如何在Node.JS项目中正确调整我的代码?
- 无法在nodeJS中validation用户:TypeError:无法使用“in”运算符在fs中search“用户名”
- 渲染包含angular度属性的玉文件
- 如何使用Node.js在Express应用程序中列出所有path,如Rails应用程序中的“rake routes”命令?