Angularfire2: Proper approach to check if user is

2019-07-27 12:06发布

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条回答
成全新的幸福
2楼-- · 2019-07-27 12:41

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.

查看更多
登录 后发表回答