【问题标题】:$authWithPassword is not a function in AngularFire 2.x$authWithPassword 不是 AngularFire 2.x 中的函数
【发布时间】:2016-12-04 20:00:58
【问题描述】:

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

基本上,我的控制器中有两个函数:authCtrl.login 和 authCtrl.register。注册对 Auth.$createUserWithEmailAndPassword() 的调用工作正常,但登录对 Auth.$authWithPassword() 的调用却没有,因为我收到“..is not a function”错误。我一辈子都想不通这里出了什么问题。

这是我的设置:

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

});

【问题讨论】:

    标签: javascript angularjs firebase angularfire firebase-authentication


    【解决方案1】:

    【讨论】:

    • 我的男人!这很有趣,因为这是我在最新版本的 AngularFire 的源代码中找到的关于 createUserWithEmailAndPassword 的内容:“* 创建一个新的电子邮件/密码用户。请注意,此函数仅创建用户,如果您*希望登录作为新创建的用户,在解决此方法的承诺后调用 $authWithPassword()。”我想我只是在它自己的函数中使用该逻辑,然后从寄存器内部调用登录函数。无论哪种方式,现在都很好用。非常感谢。
    【解决方案2】:

    也许在 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);
            });
        };
    

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 2016-12-08
      • 2020-12-10
      • 2022-01-17
      • 1970-01-01
      • 2023-04-02
      • 2020-07-06
      • 1970-01-01
      相关资源
      最近更新 更多