目前,我正在使用$on('angularFireAuth:login', function(evt,auth){...get other profile info...})
登录后填充用户的个人资料和授权详细信息。这些东西被添加到 authService 以便以后在整个应用程序中轻松访问,而无需将其放入$rootscope
. 问题是这不会在刷新或重新打开时触发。由于会话令牌或其他原因,用户仍然经过身份验证,但是他们需要重新登录才能加载其他配置文件内容。
什么是正确的解决方案?我应该使用未记录的事件来代替登录吗?
这是我正在使用的 app.js:
angular.module('myApp',['firebase']).
constant({FirebaseUrl:"https://<foo>.firebaseio.com/"}).
service('authService', function authService(){
return {
isLogged:false,
name:null,
email:null,
id:null,
group:null,
err:null
}
}).
controller('AuthCtrl', function($scope, authService, angularFireAuth, angularFireCollection, FirebaseUrl){
angularFireAuth.initialize(FirebaseUrl, {scope:$scope, name:"auth"});
$scope.login = function(){
angularFireAuth.login('password', {
email: 'test@test.foo',
password: 'test'
});
}
$scope.$on("angularFireAuth:login",function(evt,auth){
url = FirebaseUrl + 'profiles/user-' + $scope.auth.id;
$scope.url = url;
var fbref = new Firebase(url);
fbref.once('value',function(profileSnapshot){
$scope.name = profileSnapshot.child('name').val();
$scope.group = profileSnapshot.child('group').val();
});
//set authService stuff
authService.name = $scope.name;
authService.group = $scope.group;
authService.email = $scope.auth.email;
authService.id = $scope.auth.id;
authService.isLogged = true;
});
$scope.logout = function(){
angularFireAuth.logout();
authService.email = null;
authService.id = null;
authService.isLogged = false;
authService.name = null;
authService.group = null;
}
}).
controller('NavCtrl', function($scope, authService){
});