How would you implement the following flow in Angular 2?
I suppose this is a fairly standard scenario in an application requiring authentication.
The config data required by the application is defined in an injectable service.
- When the application starts (bootstraps), check localStorage.
- If localstorage contains valid JWT token, make http call to retrieve config data. When response is received and stored in the service, route to Home.
- If localstorage does not contain valid JWT token, route to login. When user submits login, http call, put received JWT in localstorage, make another http call (same as above) and when response is received and stored in the service, route to Home.
- Later, the protected components have a routerOnActivate that verifies that the config data is present in the service (meaning the user is logged in). If not, route to login.
Do you think of a better approach?
Where exactly should I implement the logic that checks the localstorage and asynchronously fetches the config via http call?
In the constructor of the App component that is bootstrapped?
How to prevent further execution of the other components (they rely on the presence of this config data)?