如何使用Node.js在Firebase中注册用户? [英] How to register a User in Firebase using Node.js?

查看:267
本文介绍了如何使用Node.js在Firebase中注册用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0)用户是在身份验证系统的Firebase中创建的(我在身份验证选项卡中看到它),

/ p>

1)但是没有对数据库进行更改。 2)该页面似乎被无限加载卡住。
$ b 3)只有开始1 ...才会被记录到控制台。






CODE:

  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;
$ b $ // Validation
req.checkBody('username','Username is required')。notEmpty();
req。 ();
req.checkBody('email','Email is not valid')isEmail();
req.checkBody('email','Email is required' ();
req.checkBody('password2','密码不匹配')。equals(req.body.password);

错误= 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 (开始2 ...);
if(error){
var errorCode = error.code;
var errorMessage = error.message;
req.flash('error_msg','注册失败,请确保所有字段都已正确填写。'+ error.message);
res.redirect('/ users / register');
console.log(创建用户的错误:,错误);
} else {
console.log(成功创建);
console.log(用uid成功创建用户:,userData.uid);
var user = {
uid:userData.uid,
email:email,
username:username
}

var userRef = firebase .database()REF( '用户/');
userRef.push()。set(user);

req.flash('success_msg','您现在注册并且可以登录');
res.redirect('/ users / login');
}

});
}
});






编辑1:

这似乎正在发生:

用户是在auth系统中创建的。该页面加载大约5分钟(很长!),然后告诉我注册失败,因为电子邮件地址已被使用(不是)。



看来用户被创建,但是注册失败,因为代码在用户被创建之后循环回来,就好像它没有被创建一样,因此创建了类型的错误:这个电子邮件地址已经在我们的数据库中了。

但是,为什么会发生这种情况?

解决方案



用户被创建,但是您没有正确处理它。没有处理成功案例(履行承诺),只有被拒绝的一个。另外,当您在发生错误时尝试写入数据库时​​,这意味着用户没有通过身份验证,这意味着如果您没有更改您的Firebase安全规则,则无法写入。 (默认的firebase安全规则可以防止非认证用户读/写数据库)

这一行:

< ()函数(函数(错误,用户数据){...

)$ b $ firefilter.auth()。createUserWithEmailAndPassword(email,password)
.catch
$ b

应改为:
$ b $ p $ firebase.auth()。createUserWithEmailAndPassword(email,密码)
.then(userData => {//成功 - 用userData做东西})
.catch(error => {//做错误的东西})



请注意,发生错误时,您将无法访问 userData ,只是错误。 / p>

希望这有助于!


PROBLEM:

0) The user is created in the auth system in Firebase (I see it in the Auth tab),

1) But no changes are made to the database.

2) The page seems to be stuck infinitely loading.

3) Only "Started 1..." is logged to the console.


CODE:

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');
            }

        });
    }
});


EDIT 1:

This is what seems to be happening:

The user is created in the auth System. The page loads for about 5 minutes (very long!), then tells me the registration failed because the email address is already in use (it was not).

It seems the user is created but the registration fails because the code loops back after the user is created as if it had not been created, therefore creating an error of type : this email address is already in our database.

But why does this happen ?

解决方案

not sure this is all, but here's what i think happens:

the user is created, but you are not handling it properly. there is no handling of a success case (fulfilled promise), only a rejected one. also, when you're trying to write to the database when an error occurs, that means the user is not authenticated, which means that if you haven't changed your firebase security rules, you won't be able to write. (the default firebase security rules prevents non-authenticated users from reading/writing to your database)

this line:

firebase.auth().createUserWithEmailAndPassword(email,password) .catch(function(error, userData) { ...

should be changed to this:

firebase.auth().createUserWithEmailAndPassword(email, password) .then(userData => { // success - do stuff with userData }) .catch(error => { // do stuff with error })

mind that when an error occurs, you won't have access to userData, just the errors.

Hope this helps!

这篇关于如何使用Node.js在Firebase中注册用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆