$authWithPassword is not a function in AngularFire 2.x

时光毁灭记忆、已成空白 提交于 2020-01-14 02:04:22

问题


I've seen posts about this problem before, but they're either outdated or offer a solution very similar to my setup.

Basically, I have two functions in my controller: authCtrl.login and authCtrl.register. The register's call to Auth.$createUserWithEmailAndPassword() works fine, but the login's call to Auth.$authWithPassword() does not, as I'm getting a "..is not a function" error. Can't for the life of me figure out what is wrong here.

Here's my setup:

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

});

回答1:


try $signInWithEmailAndPassword

See documentation: https://github.com/firebase/angularfire/blob/master/docs/reference.md#signinwithemailandpasswordemail-password




回答2:


Maybe a more complete answer building on what Aaron Saunders answered using angularjs and 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);
        });
    };


来源:https://stackoverflow.com/questions/38670049/authwithpassword-is-not-a-function-in-angularfire-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!