Amplify-js: Auth.userAttributes error?

Created on 31 May 2018  路  15Comments  路  Source: aws-amplify/amplify-js

I have Authenticator logging people in perfectly with Cognito User Pools and with Federated, which is creating a valid user full of attributes from the mapped attributes, into their respective user in Cognito User Pools. I have a lot of custom user attributes within the Cognito User Pool. After I login and have a valid user, I use UserAttributes to return me the attributes of the user.

The result is 'Cannot read property 'getSession' of undefined' - How in the world can I possibly retrieve this information. I've tried this across all three methods of login. I have all three running side by side for fallbacks and updates. (Complete Custom Login Flow using AWS Amplify Auth, Authenticator, and Hosted UI).

Auth documentation

Most helpful comment

I've been receiving the exact same error by using the following code after logging in (in case it's helpful for an additional data point):

    const currentAuthenticatedUser = Auth.currentAuthenticatedUser()
      .then(data => {
        console.log("currentAuthenticatedUser: ", data);
        const attributes = Auth.userAttributes(currentAuthenticatedUser)
          .then(data => {
            console.log("follow up attributes: ", data);
          })
          .catch(err => console.log(err));
      });

The CognitoUser object I receive back from currentAuthenticatedUser() seems to have everything set properly except Session is set to null. What's your CognitoUser object look like?

All 15 comments

Hi @michaelcuneo could you paste code snippet on how you are calling the userAttributes method? I am guessing you are not passing user object as parameter.

Thanks,
Richard

Oh. I didn't even realise that I had to send userAttributes something to get a response.

I figured it was getting the data the way the other functions do, with the cached user data, etc... So I gave it my currentsession... which appeared to be a valid user object. In this particular case I gave userAttributes this object...

{"id":"ap-southeast-2:bc76b6b9-7965-4f80-a483-0b066228deb4","email":"[email protected]","name":"Michael Cuneo"}

The response was 'TypeError: user.getSession is not a function'

I'm running v1.1.4.

Sorry I versioned the wrong program ... I'm running 0.4.1

It appears as though I can get the userAttributes from a CognitoUserPool user, but not from a Federated User, even though they are both within the User Pool as users, so I would now need to somehow manually make a UserPool user for Federated Signins, to fix the issue of User Pool users not being created when logging in with the Authenticator HOC?

I've been receiving the exact same error by using the following code after logging in (in case it's helpful for an additional data point):

    const currentAuthenticatedUser = Auth.currentAuthenticatedUser()
      .then(data => {
        console.log("currentAuthenticatedUser: ", data);
        const attributes = Auth.userAttributes(currentAuthenticatedUser)
          .then(data => {
            console.log("follow up attributes: ", data);
          })
          .catch(err => console.log(err));
      });

The CognitoUser object I receive back from currentAuthenticatedUser() seems to have everything set properly except Session is set to null. What's your CognitoUser object look like?

Experiencing the same thing.

My cognitoUser looks like this:

{username: "me-iamandrew.io", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession,聽鈥

and my code:

Auth.currentAuthenticatedUser()
      .then(user => {
        Auth.userAttributes(user)
          .then(_attributes => {
            console.log(_attributes);
          })
      })

using:

"react": "^16.2.0",
"aws-amplify": "^0.3.3",

After further investigation, I believe we might be looking for is Auth.currentUserInfo(), like so:

Auth.currentUserInfo()
      .then(res => {
        console.log(res);
      })
      .catch(err => {
        console.error(err);
      });

** Solution to this was actually to just to upgrade to aws-amplify: v4.1.0

Sorry for spamming the thread, but I also noticed this same error:

'TypeError: user.getSession is not a function'

When trying to use updateUserAttributes:

Auth.currentAuthenticatedUser()
      .then(user => {
        Auth.updateUserAttributes(user, {
          'custom:registration-step': step
        })

Any solution so far ?? I am using "aws-amplify": "^0.4.3" and "react": "^16.3.2"

Auth.currentUserInfo() doesn't return any UserAttributes. It returns an ID, an email, and a name.

KK guys solution here... it works as expected on Amplify version 0.4.3, just update the amplify to the last version simply running the install again and you can safely use Auth.currentAuthenticatedUser()... I've been working with this for the last 2 days and it's working perfectly. Hope it's helps

Sweet, thanks! I was struggling with updating custom attributes last night. I kept getting:
NotAuthorizedException A client attempted to write unauthorized attribute
After updating aws-amplify from 0.4.3 to 0.4.4 it works great. For anyone trying to use custom attributes make sure to prefix with 'custom:'
Auth.updateUserAttributes(user, {"custom:foo":"bar"})

It would be good to update this page to mention the prefix and updating to 0.4.4
https://aws.amazon.com/blogs/mobile/aws-amplify-adds-support-for-custom-attributes-in-amazon-cognito-user-pools/

Based off of the responses it seems like the problem was fixed by updating Amplify.

Going to close this for now. Please reopen if the problem still persists or you have any related questions or concerns.

Hi... came across this thread while searching for a solution to my issue. Same as @oderza .. I'm using the user object to call updateUserAttributes(user, {"custom:foo":"bar"}) but I keep getting the NotAuthorizedException

I'm using amplify version 1.1.26..

any help please?

Found the fix on StackOverflow, for any one who comes across this..

combination of the following links:

Ensure client has permissions to read & write that attribute

Prefix attribute names with 'custom'

Was this page helpful?
0 / 5 - 0 ratings