Firebase在注册时设置用户数据

时间:2014-10-28 10:34:24

标签: angularjs firebase

我想在firebase中创建一个新用户。这是我使用的代码:

function createUser(email, password, username) {
        ref.createUser({
            email: email,
            password: password
        }, function(error) {
            if (error === null) {
                $activityIndicator.stopAnimating();
                $scope.padding_error = null;
                $scope.error = null;
                logUserIn(email, password);
            } else {
                $activityIndicator.stopAnimating();
                console.log(error.code);
                switch (error.code) {
                    case "INVALID_EMAIL":
                        $scope.padding_error = "10";
                        $scope.error = "Falsche E-Mail Adresse oder falsches Passwort";
                        $scope.$apply();
                    case "INVALID_PASSWORD":
                        $scope.padding_error = "10";
                        $scope.error = "Falsche E-Mail Adresse oder falsches Passwort";
                        $scope.$apply();
                    case "EMAIL_TAKEN":
                        $scope.padding_error = "10";
                        $scope.error = "Diese E-Mail Adresse ist schon registriert";
                        $scope.$apply();
                }
            }
        });
    }

我想在我的firebase中的某处存储用户名以及电子邮件和密码。用户注册后如何立即实现?

1 个答案:

答案 0 :(得分:1)

如果注册成功,您只需将电子邮件和密码变量推送到firebase即可。请参阅下面的代码。

function createUser(email, password, username) {
        ref.createUser({
            email: email,
            password: password
        }, function(error) {
            if (error === null) {
                ... Registration successful
                $activityIndicator.stopAnimating();
                $scope.padding_error = null;
                $scope.error = null;
                ##NEWCODE HERE##
                emailRef = new Firebase("<YOURFIREBASEURL>/accounts/"+username+"/email")
                passRef = new Firebase("<YOURFIREBASEURL>/accounts/"+username+"/password")
                emailRef.set(email)
                passRef.set(password)
                logUserIn(email, password);
            } else {
                ... Something went wrong at registration
                }
            }
        });
    }