1

How do I get in Flutter, the cognito custom user attribute for user?

await Amplify.Auth.fetchUserAttributes();

returns only user attributes but not the custom defined ones.

(I have added the attribute to the schema and I am sure it's there, in the AWS UI it's there.)

radrt
  • 45
  • 7

2 Answers2

0

The fetchUserAttributes function returns a list of AuthUserAttributes including the custom ones you've defined. When you have that list you can iterate through it, and get the attributes you want.

const res = await Amplify.Auth.fetchUserAttributes();

for (var attr in res) {
  if (attr.userAttributeKey == CognitoUserAttributeKey.custom('customAttr') {
    customAttr = attr.value;
  }
}

If the custom attribute isn't there, make sure the user have that attribute.

JoakimMellonn
  • 135
  • 2
  • 9
  • This is similar to how I tried and I did not get any custom attributes. (except I inspected the attributes in the Android studio debugger). Could there be some caching thing with amplify? I set up amplify for my flutter project before adding the custom attribute to the cognito schema – radrt May 03 '22 at 13:22
  • I don't think that should make a difference, I haven't myself had problems with that. But are you absolutely sure that the user, you have signed in with, have got the custom attribute assigned. You can check this by going to the Cognito console and choosing the user pool with the name of you Amplify project. Then click on the user, and you should be able to see and edit the user attributes on this page. – JoakimMellonn May 03 '22 at 13:33
  • Yes, it shows up in the cognito aws console, but it isn't fetched by amplify. I only got 4 attributes: email_verified, identities, email, sub afairemember. – radrt May 08 '22 at 11:23
  • Okay, if a user have the custom attribute assigned, it should show up on the user's page in the cognito console. Custom attributes will have a "custom:" prefix, as an example: I have a user pool with the custom attribute "group" and it shows up as "custom:group". If the custom attribute doesn't show on the user, you will have to add it to the user, either via the console or the app. In the console on the user's page, you can press edit under "User attributes", and then add the attribute. Then it should show up when you are fetching them. – JoakimMellonn May 09 '22 at 12:15
0

The issue was that these custom attributes, after they are created, they are not by default readable or writable.

For further explanations, check https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-attribute-permissions-and-scopes Go to cognito - app clients - details - and very bottom to change permissions

Go to cognito - app clients - details - and very bottom to change permissions

radrt
  • 45
  • 7