app.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://XXXXXX.firebaseio.com/");
var auth = $firebaseAuth(ref);
var Auth = {
login: function(){
return auth.$authWithPassword({
"email": "1@1.com", //hard-coding in the email & password for testing
"password": "1"
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Login Success!", authData);
}
});
}
return Auth;
}]);
app.controller("SampleCtrl", ["$scope", "Auth", "$firebaseAuth", function($scope, Auth, $firebaseAuth) {
$scope.login = Auth.login();
}]);
我正在尝试创建一个工厂来使用angularFire用户身份验证功能
<button ng-click="login()">Login</button>
它返回错误:TypeError: Cannot read property '0' of null
我究竟做错了什么?
PLUNKER
答案 0 :(得分:1)
使用AngularFire时,使用密码提供程序进行登录调用应如下所示:
var ref = new Firebase("url","name");
var svcAuth = return $firebaseAuth(ref);
svcAuth.$authWithPassword({
email: email,
password: "******"})
.then(onLogon)
.catch(onError);
function onLogon(authData) {
console.log("Authenticated payload:", authData);
}
function onError(error) {
console.log("Authentication error:", error);
}
如果您不使用AngularFire(仅使用Firebase.js),登录的代码应该是:
var ref = new Firebase("url");
ref.authWithPassword({
email : "email",
password : "*****"
}, function(error, authData) {
if (error) {
console.log("Login Failed!", error);
} else {
console.log("Authenticated payload:", authData);
}
});
希望有所帮助。
答案 1 :(得分:0)
试试这个对我有用:
angular.module('database-operations').factory('Auth', ["$firebaseAuth", function($firebaseAuth) {
var ref = new Firebase("https://XXXXX.firebaseio.com/");
var auth = $firebaseAuth(ref);
return{
login: function(){
return auth.$authWithPassword({
"email": "1@1.com", //hard-coding in the email & password for testing
"password": "1"
}).then(function(authData){
console.log('Login successful',authData.uid);
}).catch(function(error) {
console.error("Authentication failed:", error);
});
}
}
}
]);
答案 2 :(得分:0)
app.controller("loginCtrl", ["$scope", "Auth", function($scope, Auth){
$scope.login = function(){
Auth.login().then(function(){
console.log("OK");
});
};
}]);
不知何故,这可以通过将Auth.login()
包含在函数中并使用then
来实现。
任何人都可以解释它为什么有用吗?