Reputation: 1872
I have an Angular 11 project in which I'm using AWS Amplify (aws-amplify v3.3.26) and I'm using Cognito user pools to manage users. I've set up the hosted UI and I've not used any custom attributes. Everything is working and I've selected all the attributes for read and write permissions in the user attributes in the user pool.
I'm using the following code to get the cognitoUser object:-
Auth.currentAuthenticatedUser()
.then((currentUser) => {
console.log('The currentUser => ', currentUser);
Auth.currentUserInfo()
.then((res) => {
console.log('Here are the current user info! =>', res);
})
.catch((err) => {
console.log('Current user info failed to fetch', err);
});
})
.catch((err) => {
console.log(err);
});
return;
}
According to the AWS Amplify docs I should be able to get the attributes right within the cognitoUser object. But I'm not getting it.
This is all I get (also note that session is null, not sure what that means):-
As you can see there is no "attributes" key in the object.
I also noticed that there is a Auth.currentUserInfo()
method that is available and my console log of that just results in an empty object.
And as you can see here, I have set all the required permissions for all the attributes in the user pool client definition and I don't have any custom attributes:-
Currently I am storing name and email as required attributes and all users have that. But I can't access it. I was able to see the email deeply nested within the cognitoUser object (cognitoUser.signInUserSession.idToken.payload.email
) but still can't find the name attribute. I would like to get the entire attributes object listing all available attributes of the current user.
What am I missing?
Upvotes: 6
Views: 6445
Reputation: 631
I ran into this issue as well, CognitoUser
was missing the attributes
object.
The solution was to include the aws.cognito.signin.user.admin scope both on the client-side configuration and on the App Client Settings page in the User Pool settings.
The oauth
configuration on the client side should look something like:
oauth: {
domain: "https://your-cognito-domain",
scope: ["phone", "email", "profile", "openid", "aws.cognito.signin.user.admin"],
redirectSignIn: "https://yourdomain",
redirectSignOut: "https://yourdomain",
responseType: "code",
},
The App Client settings in the AWS User Pool screen should have that enabled too.
Upvotes: 5
Reputation: 584
I use this code below to get user attributes with the same version of Amplify.
const user = await Auth.currentUserInfo();
const userId = user.attributes.sub;
If user was not verified yet e.g by clicking on verification email link, or entering verification code, you will get empty object back from Auth.currentUserInfo.
But after user was verified, you can get all attributes with the exact same method. Either by using promise chain as in code below, or if you use async/await, just use the code above.
function getUserInfo(){
return Auth.currentUserInfo()
.then((res) => {
console.log('Here are the current user info! =>', res);
})
.catch((err) => {
console.log('Current user info failed to fetch', err);
});
}
Upvotes: 2