Amplify-js: Server-side user attribute update, refresh client-side stale data

Created on 11 Dec 2018  路  2Comments  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *
Auth

* What AWS Services are you utilizing? *
Cognito

* Provide additional details e.g. code snippets *
If we update user attributes on the server-side how can we make now stale data on the client-side to be updated as well to reflect changes?

I understand when Auth.currentAuthenticatedUser() is called it checks session storage, and if user data is present there it's served without calling Cognito. But is there a way without forcefully logging a user out to refresh user data? Something like a boolean parameter passed to Auth.currentAuthenticatedUser(forceRefresh) which when true would skip the session check and fetch fresh user data directly from Cognito?

Most helpful comment

@choxnox - You have two options for accomplishing this:

  1. Call Auth.currentAuthenticatedUser() with the bypassCache option:
Auth.currentAuthenticatedUser({
    bypassCache: false  // Optional, By default is false. If set to true, this call will send a request to Cognito to get the latest user data
}).then(user => console.log(user))
.catch(err => console.log(err));
  1. Call Auth.currentUserInfo
    Amplify.Auth.currentUserInfo()
    .then((d) => {
      alert(JSON.stringify(d))
    })

Both of these should handle your use case. Of course, this requires that your client is aware of the need for refreshed data.

I'm closing this issue for now but feel free to reopen.

All 2 comments

@choxnox - You have two options for accomplishing this:

  1. Call Auth.currentAuthenticatedUser() with the bypassCache option:
Auth.currentAuthenticatedUser({
    bypassCache: false  // Optional, By default is false. If set to true, this call will send a request to Cognito to get the latest user data
}).then(user => console.log(user))
.catch(err => console.log(err));
  1. Call Auth.currentUserInfo
    Amplify.Auth.currentUserInfo()
    .then((d) => {
      alert(JSON.stringify(d))
    })

Both of these should handle your use case. Of course, this requires that your client is aware of the need for refreshed data.

I'm closing this issue for now but feel free to reopen.

Actually, I didn't know this is already supported. I've tried it and it works. Thanks.

Was this page helpful?
0 / 5 - 0 ratings