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,我做错了什么?柱塞
发布于 2014-12-30 02:10:56
app.controller("loginCtrl", ["$scope", "Auth", function($scope, Auth){
$scope.login = function(){
Auth.login().then(function(){
console.log("OK");
});
};
}]);不知何故,这是通过将Auth.login()封装在一个函数中,并在其后面加上一个then来实现的。有人能解释一下为什么会起作用吗?
发布于 2016-01-16 17:36:53
使用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);
}
});希望这能帮上忙。
发布于 2014-12-29 03:58:05
试试看它对我有用:
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);
});
}
}
}
]);https://stackoverflow.com/questions/27631439
复制相似问题