aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Unable to get updated attributes and their values from cognito #8131

Closed GM1957 closed 3 years ago

GM1957 commented 3 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

``` # Put output below this line System: OS: macOS 11.2.3 CPU: (8) x64 Apple M1 Memory: 35.78 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.16.0 - /usr/local/bin/node npm: 6.14.11 - /usr/local/bin/npm Browsers: Chrome: 90.0.4430.72 Safari: 14.0.3 npmPackages: @testing-library/jest-dom: ^5.11.10 => 5.11.10 @testing-library/react: ^11.2.5 => 11.2.5 @testing-library/user-event: ^12.8.3 => 12.8.3 aws-amplify: ^3.3.26 => 3.3.26 classnames: ^2.3.0 => 2.3.0 node-sass: ^5.0.0 => 5.0.0 prettier: ^2.2.1 => 2.2.1 react: ^17.0.2 => 17.0.2 react-dom: ^17.0.2 => 17.0.2 react-redux: ^7.2.3 => 7.2.3 react-router-dom: ^5.2.0 => 5.2.0 react-scripts: 4.0.3 => 4.0.3 react-toastify: ^7.0.3 => 7.0.3 redux: ^4.0.5 => 4.0.5 redux-devtools-extension: ^2.13.9 => 2.13.9 redux-logger: ^3.0.6 => 3.0.6 redux-thunk: ^2.3.0 => 2.3.0 sass: ^1.32.8 => 1.32.8 semantic-ui-react: ^2.0.3 => 2.0.3 web-vitals: ^1.1.1 => 1.1.1 npmGlobalPackages: npm: 6.14.11 serverless: 2.31.0 ```

Describe the bug

Unable to get current attributes and their values from Cognito

Here is the complete flow of what I did

  1. successfully logged in with aws-amplify
  2. const cognitoUser = await Auth.currentAuthenticatedUser(); console.log("cognitoUser", cognitoUser);
  3. got the Cognito user attributes info which is all correct (name, sub, email, email_verified)
  4. called a backend API to update the user info (added one more attribute "profile": 1)
  5. checked from Cognito user pool, it was successfully added and "profile": 1 was there
  6. again I did const cognitoUser = await Auth.currentAuthenticatedUser(); console.log("cognitoUser", cognitoUser);
  7. got the old result again (name, sub, email, email_verified). "profile": 1 was not there
  8. successfully signed out with aws-amplify
  9. logged in again
  10. the keys this time got from the console.log are (name, sub, email, email_verified, profile)

Expected behavior

after updating user attributes in the Cognito ( like i added a new attribute profile )

from this log

const cognitoUser = await Auth.currentAuthenticatedUser();
    console.log("cognitoUser", cognitoUser);

we should get the details of the updated attribute but got old details, after doing signout and again login this log gave the correct result

Reproduction steps

  1. successfully logged in with aws-amplify
  2. const cognitoUser = await Auth.currentAuthenticatedUser(); console.log("cognitoUser", cognitoUser);
  3. got the Cognito user attributes info which is all correct (name, sub, email, email_verified)
  4. called a backend API to update the user info (added one more attribute "profile": 1)
  5. checked from Cognito user pool, it was successfully added and "profile": 1 was there
  6. again I did const cognitoUser = await Auth.currentAuthenticatedUser(); console.log("cognitoUser", cognitoUser);
  7. got the old result again (name, sub, email, email_verified). "profile": 1 was not there
  8. successfully signed out with aws-amplify
  9. logged in again
  10. the keys this time got from the console.log are (name, sub, email, email_verified, profile)

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

const appConfig = {

aws_amplify_config: {

    // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
    identityPoolId: 'us-west-2:8e67dffe-91e6-4afd-9c39-1f16aef5341c',

    // REQUIRED - Amazon Cognito Region
    region: 'us-west-2',

    // OPTIONAL - Amazon Cognito Federated Identity Pool Region 
    // Required only if it's different from Amazon Cognito Region
    identityPoolRegion: 'us-west-2',

    // OPTIONAL - Amazon Cognito User Pool ID
    userPoolId: 'us-west-2_ZAwqBki1X',

    // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
    userPoolWebClientId: '20k2nme3ej8ujetc07k15gjip6',

    // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
    mandatorySignIn: false,

    authenticationFlowType: 'USER_PASSWORD_AUTH',

    // OPTIONAL - Manually set key value pairs that can be passed to Cognito Lambda Triggers
    clientMetadata: { myCustomKey: 'myCustomValue' },

     // OPTIONAL - Hosted UI configuration
    oauth: {
        domain: 'dev-prefixpostfix.auth.ap-southeast-1.amazoncognito.com',
        scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'],
        redirectSignIn: 'http://localhost:3000',
        redirectSignOut: 'http://localhost:3000',
        responseType: 'code' // or 'token', note that REFRESH token will only be generated when the responseType is code
    }
}

};

// You can get the current config object export default appConfig

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

reesscot commented 3 years ago

Hi @GM1957, I think the reason you are seeing old information on the second call to Auth.currentAuthenticatedUser is because the first call to get the current user is cached. To get the latest data from Cognito, you will need to call Auth.currentAuthenticatedUser with params to bypass the cache.

API references: https://aws-amplify.github.io/amplify-js/api/classes/authclass.html#currentauthenticateduser

Example:

// first call:
const cognitoUser = await Auth.currentAuthenticatedUser();

// call Cognito backend API to update the user info (added one more attribute "profile": 1)

// second call specifying that cache should be bypassed:
const updatedCognitoUser = await Auth.currentAuthenticatedUser({ bypassCache: true }) // should return user with "profile" update

Please let me know if this solves the problem.

GM1957 commented 3 years ago

Yeah @reesscot I got the solution later. Thanks for your reply

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.