My question is conceptual, so I won't write any code in here.
I am building my website with Reactjs, and all the authentication process is working properly. To make it easier, I am going to consider 03 pages in the website: - Page 1: User SignIn page using Auth.signIn method. When the user is authenticated, it automatically goes to page 2.
Page 2: In this page a lot of information from the user has to be rendered. To achieve that, there is an API where the input parameter is the username. To get the username parameter, the Auth.authenticatedUser method is called when the page is rendered. After that, the API is called to get the user information from the database. Lets suppose there is only a Button in the page 2 that gives access to page 3.
Page 3: in this page there are more informations about the user, that has to be read from the database. The API that gets theses information has again the input parameter being the "username". To get the username parameter, I have 2 options: Option 1: Use again the Auth.authenticatedUser and get the username Option 2: Pass the "username" from page 2 to page 3 as a props.
In my case, using the option 1 it is much easier, because my website have many pages, and pass props from one page to another is being a difficult task - I will end up having to use redux.
My question is: - Is there any problem to use Auth.authenticatedUser in all pages to get the username information? There is any disvantage by doing that (i.e speed to render the page, etc...)
From the source it looks like Amplify caches the user both in memory and in localStorage so retrieving the username should be fairly efficient.
Just double-check in your browser's network tab that Amplify doesn't make any network requests every time you call Auth.currentAuthenticatedUser
.
From the looks of it, Amplify seems to have been developed with your use-case in mind in that it can be used almost like a "global store" of a user's authentication information. Instead of just being a wrapper around an API, Amplify does some caching and logic for you. So it's probably better to just call it directly instead of storing the user's data in Redux or passing it among pages. Storing the user's data in Redux or passing it among pages will just create duplicate logic.