$ createUser无效

时间:2014-10-30 09:20:53

标签: angularjs firebase firebasesimplelogin

我使用带有firebase的AngularJS用于firebaseSimpleLogin。它工作了一段时间,突然它不起作用。登录部分工作正常,但新用户无法注册。

var userAuthRef = new Firebase(URL+"/userAuth");
$scope.loginObj = $firebaseSimpleLogin(userAuthRef);

$scope.userSignup = function(){
        $('.signup.small.modal').modal('hide');
        if ($scope.Password!="" && angular.equals($scope.PasswordFirst, $scope.PasswordConfirm)){
            console.log($scope.Email + $scope.PasswordConfirm);  ////the first log
            $scope.loginObj.$createUser($scope.Email,$scope.PasswordConfirm)
            .then(function(auth) {
                console.log("reached here");   ////the second log
                if (auth === null) {
                    console.log("Error creating user:", error);
                } else {
                    console.log("User created successfully");
                    $scope.loginObj.$login("password", {
                        email: $scope.Email,
                        password: $scope.PasswordConfirm
                    }).then(function(user) {
                        console.log("Logged in as: ", user.uid);
                        userRef = $firebase(new Firebase(someURL+user.uid));
                        userRef.$set({username: $scope.Username, time: Date(), reported: 0});
                        $scope.user = userRef.$asObject();
                    }, function(error) {
                        console.error("Login failed: ", error);
                    });
                }
            }), function(error){
                console.error("signup failed: ", error);
                ////Sign up failure log
            };
        }
        else{
            console.log("condition does not satisfy");
        }
    };

我很确定函数userSignup()被调用,因为第一个console.log确实打印出了一些东西。然后我甚至不知道是否执行$ createUser,因为控制台不再提供任何内容。

以下是Firebase forge的规则:

{
  "rules": {
    ".read": true,
    "$user": {
        ".write": true
    }
  }
}

$ createUser的使用是否错误?

1 个答案:

答案 0 :(得分:1)

我认为出现此问题是因为FirebaseSimpleLogin现在是标准firebase库的一部分。请参阅此处,不推荐使用:https://www.firebase.com/docs/web/api/firebasesimplelogin/

从现在开始使用这里描述的api: https://www.firebase.com/docs/web/api/firebase/createUser.html

如果您仍希望对象包含有关身份验证的数据,请使用.getAuth(): https://www.firebase.com/docs/web/api/firebase/getAuth.html

尝试以下代码:

var userAuthRef = new Firebase(URL);
$scope.userSignup = function() {
    if ($scope.Password != "" && angular.equals($scope.PasswordFirst, $scope.PasswordConfirm)) {
        $('.signup.small.modal').modal('hide');
        console.log($scope.Email + $scope.PasswordConfirm); ////the first log
        firebaseRef.createUser({
            email: $scope.Email,
            password: $scope.PasswordConfirm
        }, function(err) {
            console.log("reached here"); ////the second log
            if (err) {
                switch (err.code) {
                    case 'EMAIL_TAKEN':
                        console.log("Error creating user, email taken:", error);
                    case 'INVALID_EMAIL':
                        console.log("Error creating user, invalid email:", error);
                    case
                    default:
                        console.log("Error creating user:", error);
                }
            } else {
                // User account created successfully!
                console.log("User created successfully");
                dataRef.authWithPassword({
                    "email": $scope.Email,
                    "password": $scope.PasswordConfirm
                }, function(error, authData) {
                    if (error) {
                        console.log('Login Failed!', error);
                    } else {
                        console.log('Authenticated successfully with payload:', authData);
                        console.log("Logged in as: ", authData.uid);
                        var userRef = $firebase(new Firebase(someURL + authData.uid));
                        userRef.$set({
                            username: $scope.Username,
                            time: Date(),
                            reported: 0
                        });
                        $scope.user = userRef.$asObject();
                    }
                });
            }
        });
    }
}