-1

Problem description

In my app I'm using JWT authentication. Inside the payload of the token I store the user id. After the login, or when the user refresh the page and still have a valid token, I use the id inside the token to retrieve some information about the user from my backend. I need to share those information across multiple components of the app that rely on them.

Current solution

I have a service that retrieves and stores those information in an object. The components that rely on those information to work properly have this service injected inside them (service is a singleton) and have an *ngIf on top of their layout to avoid displaying anything if the information are not present.

Possible alternative solution (Route Resolves)

I recently found out about route resolves guards. Could those be useful in my case ? I could implement a guard that requires the information to be loaded before display the page, and add it to all the routes that requires them.

  • Do those guards work like a service ?
  • Can those be injected in the components? Are they singletons ?
  • If I return the object containing the user information to the route (I would store the object inside the route guard after the first load, to avoid unnecessary api calls), will this be the same object present inside the guard or a copy ? (this in case I needed to modify it) (As described in this tutorial http://www.callibrity.com/blog/angular-2-route-resolves)

Thank you

Nite
  • 323
  • 1
  • 4
  • 12

1 Answers1

0

I hope my answer to this question helps you

Ques: Is there a way to resolve data for an Angular 4 service before it's injected into a component?

Ans: https://stackoverflow.com/a/47507341/6926892

Aseem Khan
  • 124
  • 1
  • 7