Angularfire2: Proper approach to check if user is logged in?

丶灬走出姿态 提交于 2020-01-05 05:46:05

问题


I wan't to allow only logged in users to access a certain site. What's the proper way of checking if a user is logged in on load?

The code below is my first try, don't really know if it works though. Of course I already wrote db rules, now I want to complete the behavior visually for the user.

this.af.auth.subscribe(auth => {
   if (auth) {
      //route to other view
   } else {
      //do whatever
   }
});

回答1:


You should consider use angular 2 guards, like canActivate. They can help you to do validation before the routing happens. This way you can prevent the navigation of unauthorized users.

As say early, this approach works. If you want do transformation over the auth information. Probably, you should use observables methods like map, flatmap, concatMap.



来源:https://stackoverflow.com/questions/39205045/angularfire2-proper-approach-to-check-if-user-is-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!