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

1.6k views Asked by At

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?

2

There are 2 answers

1
Jan On BEST ANSWER

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": 
...
1
HackRx On

I was trying to get user attributes in a flutter project by using flutter amplify package, here is how you can access all the attributes:

var res = await Amplify.Auth.fetchUserAttributes();
    for (int i = 0; i < res.length; i++) {
        print("${res[i].userAttributeKey} :: ${res[i].value}");
      }

!