Best practice: Sharing user credentials across all pages in app


#1

I’m wondering what would be the smartest way to share the user’s credentials with the pages of your app. Let’s take the userId for example. That’s something that you probably need on almost any page of your app.

Do you call the backend server on any page then to authenticate the user and receive the current user’s Id (which is somehow time consuming and relying on a stable connection)? Or do you somehow authenticate the user once and then share the credentials with all pages of your app?

I thought about receiving the credentials once (in app.components.ts) and then share it using events. But this didn’t really work out…

How do you deal with this?

EDIT: Just re-read this post. It indeed sounds like I want to call the backend directly from the pages. Of course I didn’t intend to do this. I meant calling the provider to communicate with the backend instead.


#2

Pages should not have to care about this at all. All authentication stuff should be handled by providers.


#3

Yes sure. But on pages we need to show content depending on the user. Hence, we need to know credentials like the userId on the page to be able to show the proper content.

The question is, if I call the provider to authenticate the user on each page separately or if I share the information somehow differently.


#4

You authenticate once, then the provider knows about it, and you can get the data from the provider every where you want.


#5

Check this documentation. This will help you.