无法在 AngularFire 0.9.0 中使用新的密码身份验证方法? [英] Can't get new password authentication methods to work in AngularFire 0.9.0?
问题描述
我正在尝试在 Angular Fire 0.9.0 中使用新的身份验证方法,但我一定是做错了什么.
I'm trying to use the new authentication methods in Angular Fire 0.9.0 but I must be doing something wrong.
我正在运行 1.3.2 的 Angular、2.0.4 的 Firebase 和 0.9.0 的 Angular Fire.
I'm running 1.3.2 of Angular, 2.0.4 of Firebase and 0.9.0 of Angular Fire.
我从 html 中的 ng-click
调用登录功能.
I call the login function from an ng-click
in my html.
这是我的js:
var app = angular.module("sampleApp", ["firebase"]);
app.controller('mainCtrl', function ($scope, $firebaseAuth) {
var ref = new Firebase('https://XXXX.xxx');
$scope.auth = $firebaseAuth(ref);
$scope.login = function() {
$scope.num = 'loggin in';
$scope.auth.$authWithPassword({
email: 'xxx@xxx.xxx',
password: 'yyyyy'
}, function(err, authData) {
if (err) {
console.log(err);
$scope.num = err;
} else {
console.log(authData);
}
});
};
我在控制台中没有看到任何内容,也没有收到任何错误.所以我不知道如何调试我做错了什么.
I don't see anything in the console and I don't get any errors. So I can't figure out how to debug what I am doing wrong.
当我将 $scope.auth
登录到控制台时,它会显示 $authWithPassword
方法.我就是无法让它工作.
When I log $scope.auth
to the console, it shows the $authWithPassword
method. I just can't get it to work.
任何帮助将不胜感激.
推荐答案
您遇到的问题是 AngularFire API 与用于身份验证方法的常规 Firebase API 略有不同.虽然 Firebase SDK 将回调作为 authWithPassword() 的第二个参数,但 AngularFire API 会返回 $authWithPassword() 的承诺.不同的原因是承诺是 Angular 中非常常见的习惯用法,我们希望提供一个人们已经熟悉的 API.因此,您的代码应如下所示:
The problem you are running into is that the AngularFire API is slightly different than the regular Firebase API for the authentication methods. While the Firebase SDK takes a callback as the second argument for authWithPassword(), the AngularFire API returns a promise for $authWithPassword(). The reason for the difference is that promises are a very common idiom in Angular and we wanted to provide an API that people are already familiar with. So, your code should look like this:
var app = angular.module("sampleApp", ["firebase"]);
app.controller('mainCtrl', function ($scope, $firebaseAuth) {
var ref = new Firebase('https://XXXX.xxx');
$scope.auth = $firebaseAuth(ref);
$scope.login = function() {
$scope.num = 'logging in';
$scope.auth.$authWithPassword({
email: 'xxx@xxx.xxx',
password: 'yyyyy'
}).then(function(authData) {
console.log(authData);
}).catch(function(error) {
console.log(err);
$scope.num = err;
});
};
});
这篇关于无法在 AngularFire 0.9.0 中使用新的密码身份验证方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!