'Auth.currentAuthenticatedUser not loading name and family name attributes (and others) from Cognito

I'm using the Auth.currentAuthenticatedUser method to retrieve the attributes recorded for the logged user from AWS Cognito - but only basic atributes are showing. The ones I want are "name" and "family name", but they don't seem to be loaded in the Promise.

This is only the beggining, but I'm concerned as I will want to retrieve other attributes which are not showing up, like user picture, for instance.

Tried to use currentAuthenticatedUser and currentUserInfo with the same results.

  async componentDidMount() {

    await Auth.currentAuthenticatedUser({bypassCache: true})
                          .then ( user => this.setUserInfo( user ) )
                          .catch( err => console.log(err))


  }

CognitoUser {
  "Session": null,
  "attributes": Object {
    "email": "[email protected]",
    "email_verified": true,
    "phone_number": "+5...",
    "phone_number_verified": false,
    "sub": "246e9...",
  },
  "authenticationFlowType": "USER_SRP_AUTH",
  "client": Client {
    "endpoint": "https://cognito-idp.us-east-2.amazonaws.com/",
    "userAgent": "aws-amplify/0.1.x react-native",
  },
  "deviceKey": undefined,
  "keyPrefix": "CognitoIdentityServiceProvider.12ddetjn0c0jo0npi6lrec63a7",
  "pool": CognitoUserPool {
    "advancedSecurityDataCollectionFlag": true,
    "client": Client {
      "endpoint": "https://cognito-idp.us-east-2.amazonaws.com/",
      "userAgent": "aws-amplify/0.1.x react-native",
    },
    "clientId": "12ddetjn0c0jo0npi6lrec63a7",
    "storage": [Function MemoryStorage],
    "userPoolId": "us-east...",
  },
  "preferredMFA": "NOMFA",
  "signInUserSession": CognitoUserSession {
    "accessToken": CognitoAccessToken {
      "jwtToken": "e...oJPg",
      "payload": Object {
        "auth_time": 1565137817,
        "client_id": "1...6lrec63a7",
        "event_id": "c3...-4bd9-ad42-200f95f9921c",
        "exp": 15...2,
        "iat": 156...5872,
        "iss": "https://cognito-idp.us-east-2.amazonaws.com/us-east-...",
        "jti": "5483e...544149c42e58",
        "scope": "aws.cognito.signin.user.admin",
        "sub": "246e93...f4d8e6f4725b",
        "token_use": "access",
        "username": "r...f",
      },
    },
    "clockDrift": -2,
    "idToken": CognitoIdToken {
      "jwtToken": "eyJraWQiOiJk...",
      "payload": Object {
        "aud": "12ddetjn0c0j..rec63a7",
        "auth_time": 1565137817,
        "cognito:username": "r..",
        "email": "[email protected]",
        "email_verified": true,
        "event_id": "c3ae..200f95f9921c",
        "exp": ..2,
        "iat": ..2,
        "iss": "https://cognito-idp.us-east-2.amazonaws.com/us-east-..",
        "phone_number": "+5...3",
        "phone_number_verified": false,
        "sub": "246e937..f4d8e6f4725b",
        "token_use": "id",
      },
    },
    "refreshToken": CognitoRefreshToken {
      "token": "eyJjd...",
    },
  },
  "storage": [Function MemoryStorage],
  "userDataKey": "CognitoIdentityServiceProvider.12ddetjn0....userData",
  "username": "r...ff",
}


Solution 1:[1]

To get all user attributes, you may be looking for the Auth.userAttributes() function. To use this you want something like this code:

  const authUser = await Auth.currentAuthenticatedUser();
  const attributes = await Auth.userAttributes(authUser);
  // the next line is a convenience that moves the attributes into
  // the authUser object
  attributes.forEach((attr) => {
    authUser.attributes[attr.Name] = attr.Value;
  });

If you're still not getting the attributes you need, take a look here, and you can see that you can enable the reading of other attributes from the Amplify command line.

So, in the root of your project:

  1. Type "amplify update auth" at the console.
  2. Select "Walkthrough the auth configurations"
  3. Step through making all the same selections as you've done before.
  4. When it asks, "Do you want to specify the user attributes this app can read and write?" it's "Y", and then you select the attributes you want to be able to read.
  5. When you finish the wizard, use "amplify push auth"
  6. When that's completed, try re-running.

As an alternative to steps 1-4 above, you can also edit cli-inputs.json in the amplify\backend\auth<your auth config name> directory. It's in "userpoolClientReadAttributes". Simply add the attributes you would like to this array (e.g. "name").

This answer was verified with amplify CLI version 8.1.0.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben Sewell