2

I have a design question about when and where to send multiple HTTP requests for initialization information (types, business dates etc) that will be displayed in all components.

Right now I have my auth.guard that after the user signs in retrieves the user claims and authorization rules and then checks if the user is authorized. The guard returns true or false based on their roles. From here I have around 10 info HTTP calls that are the same for most of my components. But I want to keep these HTTP calls out of my auth.guard since they can run in the background as the component loads and it should not be the auth.guards responsibility.

I am also using a shared service that uses BehaviorSubjects so multiple components can subscribe to the value.

So, my main question is, what is the best practice so that after the auth.guard returns true, run x amount of HTTP requests and update the shared service. This needs to be applied to any number of components.

Also been looking at route resolvers which might solve the issue, but I would rather save the data to a shared service instead of return the data to the component and have to wait.

Another piece that makes this a little more complicated is that I have my 10 HTTP calls spread throughout multiple services, so if I have a shared service it would need to inject all of the other services.

DevEng
  • 1,114
  • 2
  • 15
  • 29
  • just run them in the component constructor? – mast3rd3mon May 24 '18 at 15:25
  • 1
    @mast3rd3mon I only want to run these HTTP requests **once** after login. So If I navigate from component to component don't rerun the requests. Plus, they are the same so I want to have it in one place to reduce code duplication – DevEng May 24 '18 at 15:28
  • then have a global service in which you call them? – mast3rd3mon May 24 '18 at 15:28
  • https://stackoverflow.com/questions/41619443/how-to-call-an-rest-api-while-bootstrapping-angular-2-app?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – dee zg May 24 '18 at 16:08
  • @deezg this happens after the user logins, so a good time after app Initialization. The user needs to be authenticated to get the http responses. – DevEng May 24 '18 at 18:34
  • aha...well then maybe a `canDeactivate` of the login component might be a good candidate? Triggering a global service at that point. – dee zg May 24 '18 at 18:50

0 Answers0