The documentation for amplify auth is still very poor, so I looked into the source code for @aws-amplify/auth
and amazon-cognito-identity-js
packages and these are the findings:
currentAuthenticatedUser
will try to retrieve authenticated user info from localstorage (unless your storage options is configured otherwise). If it doesn't exist in storage, then it will make api calls to retrieve user info which involves automatically refreshing the user session in the process.
currentSession
will not check the local storage and always invoke the API which also involves automatically refreshing the user session if expired.
So to answer your question directly, the Auth.currentAuthenticatedUser()
method doesn't always give you a valid token. If your storage contains an expired token, it will just return that. This would require you to call user.getSession()
on the returned user object to request for a new session/token manually. I recommend that you use Auth.currentSession()
since this handles the token refresh automatically and always returns a valid token.