$authWithPassword 不是 AngularFire 中的函数 2.x

$authWithPassword is not a function in AngularFire 2.x

我以前看过关于这个问题的帖子,但它们要么已经过时,要么提供的解决方案与我的设置非常相似。

基本上,我的控制器中有两个函数:authCtrl.login 和 authCtrl.register。寄存器对 Auth.$createUserWithEmailAndPassword() 的调用工作正常,但登录对 Auth.$authWithPassword() 的调用没有,因为我收到“.. 不是函数”错误。我这辈子都弄不明白这里出了什么问题。

这是我的设置:

auth.service.js:

angular.module('angularfireSlackApp')
.factory('Auth', function($firebaseAuth, $firebaseObject){
var auth = $firebaseAuth();

return auth;
});

auth.controller.js:

angular.module('angularfireSlackApp')
.controller('AuthCtrl', function(Auth, $state){
var authCtrl = this;

authCtrl.user = {
  email: '',
  password: ''
};

authCtrl.login = function() {
// Why is this not a function
Auth.$authWithPassword(authCtrl.user)
.then(function (auth){
  $state.go('home');
}, function (error){
  authCtrl.error = error;
});
}

authCtrl.register = function() {
Auth.$createUserWithEmailAndPassword(authCtrl.user.email, authCtrl.user.password)
.then(function (user){
  authCtrl.login();
}, function (error){
  authCtrl.error = error;
});
}

});

尝试$signInWithEmailAndPassword

查看文档: https://github.com/firebase/angularfire/blob/master/docs/reference.md#signinwithemailandpasswordemail-password

也许更完整的答案建立在 Aaron Saunders 使用 angularjs 和 angularfire 回答的基础上:

var auth = $firebaseAuth();

$scope.loginUser = function (email, password) {
        auth.$signInWithEmailAndPassword(email, password).then(function(firebaseUser) {
            console.log("User Logged in successfully with uid: " + firebaseUser.uid);
        }).catch(function(error) {
            console.log("An Authentication error occurred: " + error);
        });
    };