Amiga500
Amiga500

Reputation: 6131

AWS managing users via Cognito

I have used many services from AWS, some were easy, while some were a bit difficult. After 2 days of searching everywhere, I can say documentation for this service is misleading.

I have simple task to do. I want to change a user attribute in the Cognito pool. And to make things easy, I just need to change an Email, and thats it. Application is an Backoffice (Express/Node), where admins can change user's email.

After reading and reading, I am getting more confused. Apparently, the aws-sdk library, the one I am familiar with, has some Cognito API's that I could use. Getting a working example on how to use them, turned out to be a nightmare. Then I found out there is a library, but only to be used on the client side. After some tweaks I got it running in Node.js. The tweak was to expose a fetch library in global Node.js namespace.

I was able to add a new user. But for all my intentions, I can't change any of the attributes (like email). The library wants me to provide Username (real user) and a password. I do have a Username (in this case an email), but I don't have the password.

All I need to do is to connect to the service, and send new attribute for the user and thats it. This is what I have so far (mainly hacked code samples, from variety of places), and I cant get it to work:

var poolData = {
    UserPoolId : 'euXXXXXXX',
    ClientId : 'XXXXXXXXXXXX'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

Ok The above line makes a connection to the existing user pool.

Now if I were to do this:

var attributeList = [];

var dataEmail = {
    Name : 'email',
    Value : '[email protected]'
};

var dataPhoneNumber = {
    Name : 'phone_number',
    Value : '+15555555555'
};

var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
var attributePhoneNumber = new AmazonCognitoIdentity.CognitoUserAttribute(dataPhoneNumber);

attributeList.push(attributeEmail);
attributeList.push(attributePhoneNumber);

userPool.signUp('username', 'password', attributeList, null, function(err, result){
    if (err) {
        alert(err.message || JSON.stringify(err));
        return;
    }
    cognitoUser = result.user;
    console.log('user name is ' + cognitoUser.getUsername());
});

I can see in AWS console that the user is being added. Great.

Now how to change the attributes of the existing user? All of examples like this and this Suggest the following:

Use case 8. Update user attributes for an authenticated user.

var attributeList = [];
var attribute = {
    Name : 'nickname',
    Value : 'joe'
};
var attribute = new AmazonCognitoIdentity.CognitoUserAttribute(attribute);
attributeList.push(attribute);

cognitoUser.updateAttributes(attributeList, function(err, result) {
    if (err) {
        alert(err.message || JSON.stringify(err));
        return;
    }
    console.log('call result: ' + result);
});

The problem here is I cant authenticate the user. I can't know user's password, only his email. This is after all a simple Backoffice program, where I just need to change users email.

What can I do in this case?

Upvotes: 4

Views: 3355

Answers (1)

robbannn
robbannn

Reputation: 5013

To update the attributes of a Cognito User Pool-user as an admin, you should use adminUpdateUserAttributes function from the aws-sdk class CognitoIdentityServiceProvider.

let AWS = require('aws-sdk');
let cognitoISP = new AWS.CognitoIdentityServiceProvider({ region: 'your-region-here' });

function updateUserAttribute(name, value, username, userPoolId){
    return new Promise((resolve, reject) => {
        let params = {
            UserAttributes: [
                {
                    Name: name,     // name of attribute
                    Value: value    // the new attribute value
                }
            ],
            UserPoolId: userPoolId,
            Username: username
        };

        cognitoISP.adminUpdateUserAttributes(params, (err, data) => err ? reject(err) : resolve(data));
    });
}

Upvotes: 5

Related Questions