在页面重新加载Firebase中处理异步验证以获取需要用户的uid的列表 [英] Handle asynchronous authentication in Firebase on page reload to get list that needs user's uid
问题描述
使用Angularjs。如何处理人员重新加载页面的情况,并且在触发Firebase身份验证事件之前,控制器尝试使用当前用户的uid查询Firebase并有机会设置当前用户?
详细信息:
Firebase查询需要的是用户的uid,当通过$ state.go('/ list')导航到路由时,所有内容都可以正常工作。
但是,当一个人重新加载页面时,控制器会检查用户,直到事件处理程序触发为止:
auth.onAuthStateChanged(function(user){
if(user){
//设置用户
UserSrvc.currentUser = user;
} else {
$ state .go('/ login');
}
});
$ b app.controller('ListCtrl',function($ scope,UserSrvc,Posts){
if(!UserSrvc.currentUser){
UserSrvc.getUser();
};
$ scope.posts = Posts.list();< - 这个生成错误,因为没有用户是$ b $如果Firebase仍然具有同步认证调用,那么这不会是一个问题,因为我可以
所以:
- >
- 如果用户没有登录,UserSrvc.getUser()如何导航到$ state.go('/ login')而没有Posts.list()执行?
解决方案将帖子加载到 onAuthStateChanged()
:
app.controller 'ListCtrl',函数($ scope,UserSrvc,Posts){
auth.onAuthStateC (用户){$超时(函数(){
如果(用户){
UserSrvc.currentUser = user;
if(!UserSrvc.currentUser){
UserSrvc.getUser();
};
$ scope.posts = Posts.list();
} else {
$ state.go('/ login');
}
})});
});
你会注意到我添加了一个 $ timeout()
,否则Angular将不会意识到我们已经对 $ scope
进行了更新。
请注意,我们有专门的将Firebase绑定到AngularJS的库,名为AngularFire ,它可以完成很多工作自动为您。
Using Angularjs. How do you handle the situation where a person reloads a page and the controller tries to query Firebase using the current user's uid before the Firebase auth event is triggered and has a chance to set the current user?
Details:
The Firebase query require's the user's uid and everything works when navigated to the route via $state.go('/list')
But when a person reloads the page the controller checks for the user which doesn't exist until this event handler fires:
auth.onAuthStateChanged(function(user) {
if (user) {
//Set user
UserSrvc.currentUser = user;
} else {
$state.go('/login');
}
});
app.controller('ListCtrl', function($scope, UserSrvc, Posts ) {
if (!UserSrvc.currentUser){
UserSrvc.getUser();
};
$scope.posts = Posts.list(); <--THIS GENERATES ERROR, SINCE NO USER YET
})
If Firebase still had the synchronous auth call this wouldn't be an issue since I could do the check and call at the beginning of the controller.
So:
- What is the best way to handle this situation.
- If user is not logged in, how does UserSrvc.getUser() navigate to $state.go('/login') without having Posts.list() execute?
解决方案 The common way to implement this is by moving the loading of the posts into onAuthStateChanged()
:
app.controller('ListCtrl', function($scope, UserSrvc, Posts ) {
auth.onAuthStateChanged(function(user) { $timeout(function() {
if (user) {
UserSrvc.currentUser = user;
if (!UserSrvc.currentUser){
UserSrvc.getUser();
};
$scope.posts = Posts.list();
} else {
$state.go('/login');
}
})});
});
You'll note I added a $timeout()
in there, since otherwise Angular won't be aware of the update we've made to $scope
.
Note that we have a dedicated library binding Firebase to AngularJS called AngularFire, which does a lot of this automatically for you.
这篇关于在页面重新加载Firebase中处理异步验证以获取需要用户的uid的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!