教程:http://www.thinkster.io/angularjs/wBhtRLWHIR/6-authenticating-users-with-a-service
我遵循本教程,似乎我在注册后就失去了我的用户。
这是我的auth.js工厂:
'use strict';
app.factory('Auth', function($firebaseSimpleLogin, FIREBASE_URL, $rootScope){
var ref = new Firebase(FIREBASE_URL);
var auth = $firebaseSimpleLogin(ref);
var Auth = {
register : function(user) {
return auth.$createUser(user.email, user.password);
},
signedIn : function() {
// PROBLEM: authUser is always null
console.log(auth.user);
return auth.user !== null;
},
logout : function () {
auth.$logout();
}
};
$rootScope.signedIn = function () {
return Auth.signedIn();
};
return Auth;
});
这是我的auth.js控制器:
'use strict';
app.controller('AuthCtrl', function($scope, $location, Auth){
if (Auth.signedIn()) {
$location.path('/');
}
$scope.register = function () {
Auth.register($scope.user).then(function (authUser) {
console.log(authUser);
$location.path('/');
});
};
});
工厂中signedIn下的console.log始终为null。知道断开的地方吗?注册本身工作正常,注册时控制器中的console.log中填充了authUser
。
答案 0 :(得分:2)
The latest documentation for Angularfire表示$createUser
的{{1}}方法会返回一个承诺,但它没有提及传递给$firebaseSimpleLogin
的任何参数回调。
您可以使用$getCurrentUser方法在用户注册后获取当前用户。
教程需要更新,您总是检查您自己使用的库的文档。
您的signedIn代码应如下所示:
then
答案 1 :(得分:1)
我在教程中发现了一个非常类似的问题:can't show logout button after $createUser
在答案中,我了解到angularfire用于在用户创建后自动登录。显然现在它不再那样,这就是为什么signedIn中的auth.user为空。
答案 2 :(得分:0)
我现在正在做相同的教程。这段代码(在auth控制器中)对我有用:
$scope.register = function () {
Auth.register($scope.user).then(function (authUser) {
console.log(authUser);
Auth.login($scope.user);
$location.path('/');
});
};
我总共n00b,但是(我认为)这样做的是验证用户身份,然后运行一个记录用户的功能。注销按钮现在正常运行。