问题
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