Slowly but surely progressing with Angular2. And now I faced the following challenge. I want to check if the user logged in or not on every page change (in other words on load of each and every component). Of course, I can implement OnInit interface in each and every one of them, but that is code smell.
Is there any efficient way of executing anything that needed on every page of the app? I would love to hear any other suggestions about best practices of how to handle this task.
I am using this library (https://auth0.com/blog/2015/11/10/introducing-angular2-jwt-a-library-for-angular2-authentication/) for jwt based login and I already have a nice service class that encapsulates all authentication related functionality. So, the actual checking where or not the user is logged in is already done and tested.
Thanks,
If you use routing (and it seems to be the case since you say: "on every page change"), you can leverage several things:
Create a custom router-outlet (a sub class of
RouterOutlet
) that checks authentication with itsactivate
method is called. In this case, you can have something global. Something like that:See this question for more details:
Leverage the
CanActivate
decorator to check is a component can be activated or not. In your case, you can execute authentication checking at this level.Something could also be done at the RouterLink level to show / hide route links. In this case, you can apply roles on these links based on related route configuration and current user hints. See this question for more details:
This can be also handled within an HTTP interceptor (a class that extends the
Http
one). In this case, when a request is executing, you can plug some authentication checks:See this question for more details:
I'm showing you simple implementation with Angular2. You can take advantage of @CanActivate hook as shown blow to check whether the user is loggedIn or not with isLoggedIn function which returns promise.
NOTE: below implementation is to check whether user is
loggedIn
before accessing any component or not. I hope by some modification you can achieve what you want to have.Auth.ts
isLoggedIn.ts
appInjector.ts
somecomponent.ts
boot.ts
There are two ways to restrict access
Custom Router Outlet
andCanActivate Decorator
shown and implemented in this great article Authentication in Angular 2I think extending RouterOutlet is a common way to achive this
Example posted a while ago in Gitter by CaptainCodeman (not tested myself yet)