Search code examples
amazon-web-servicesoauth-2.0amazon-cognitoaws-amplify

How to receive attributes from users who signed in via an identity provider in AWS Amplify?


I want to allow my users to sign in with an identify provider using AWS Cognito.

import { Auth } from 'aws-amplify';
...

Auth.federatedSignIn({ provider: "Google" })};

This actually works fine. After a user signs in, the user gets also listed in the user pool in the AWS console with the desired attributes - hence the attribute mapping seems to work as well.

However, when Auth.currentAuthenticatedUser() gets called in my frontend while a user is sign in via federation, the returned object doesn't contain the users attributes - this property is somehow missing.

import { Auth } from 'aws-amplify';
...
const user = await Auth.currentAuthenticatedUser();
const { attributes } = user;
console.log('current attributes:', attributes);

returns => current attributes: undefined

The same method does return the attributes for users who aren't using a federation service.

current attributes: 
{
    email: ...
    given_name ...
}

How can I make Cognito return the attributes also for users which signed in via an identify provider, so that I e.g. can display the full name of the user?


Solution

  • I found the attributes deeply nested in the returned object (e.g. given_name):

    {
      "username": "Google_xy",
      "pool": {
        "userPoolId": ...,
        "clientId": ...,
        "client": {
          ...
        },
      
      "signInUserSession": {
        "idToken": {
          "jwtToken": ...
          "payload": {
            "at_hash": ...,
            "sub": ...,
            "cognito:groups": [
              ...
            ],
            "email_verified": ...,
            "iss": ...,
            "cognito:username": ...,
            "given_name": ...,
            "nonce": ...,
            "aud": ...,
            "identities": [
              ...
            ],...
          }
        },
        "refreshToken": {
          "token": ""
        },
        "accessToken": {
          "jwtToken": 
    ...