Hey Jeremy,

as you correctly stated, checking a user's token is an asynchronous task, so you can't know ahead of time if the user is or should be authenticated.

There are a 3 core options to explore:

- You could save the user's core data (email, username) in the localStorage and then

optimistically assume that the user is authenticated while `Auth.currentAuthenticatedUser()` is still in-flight and hasn't resolved. When it resolves you can update the state accordingly.

This works well when the chances of an invalid token are small (big expiry date), but may lead to unfriendly UI if the token expiration is short, since users will briefly be considered authenticated (and see the authenticated user's UI), before being redirected to the sign-in screen (when `Auth.currentAuthenticatedUser()` returns)

- You can delay initial rendering oof the app (showing a splash logo) until the `Auth.currentAuthenticatedUser()` request resolves. That's what most native apps do, but you can also see it in apps like InVision. This way the initial value of `null` won't affect you

- The last option is to call `Auth.currentAuthenticatedUser()` in a NodeJS backend and pre-populate the value for the client code. This is the hardest, especially since Amplify doesn't work well with in non-browser land.

Generally, my article was aimed to be a "good start", rather than a production ready code to use. Hopefully my answers above helped out.

Feel free to reach out again if you have any further questions.

Cheers!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store