Angularfire2:正确的方法来检查用户是否登录? [英] Angularfire2: Proper approach to check if user is logged in?

查看:185
本文介绍了Angularfire2:正确的方法来检查用户是否登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想只允许登录的用户访问某个站点。
检查用户是否正在登录的正确方法是什么?



下面的代码是我第一次尝试,不知道它是否工作虽然。
当然,我已经写了数据库规则,现在我想要为用户直观地完成行为。


$ b

  this.af.auth.subscribe(auth => {
if(auth){
// route to other view
} else {
//做任何
}
});


解决方案

您应该考虑使用角2守卫, href =https://angular.io/docs/ts/latest/api/router/index/CanActivate-interface.html =nofollow> canActivate 。他们可以帮助您在路由发生之前进行验证。这样可以防止未经授权的用户导航。

早就说,这种方法是有效的。如果你想对auth信息进行转换。可能你应该使用象map,flatmap,concatMap这样的可观察方法。


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
   }
});

解决方案

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.

这篇关于Angularfire2:正确的方法来检查用户是否登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆