Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the Cognito user in Node.js?

How to delete user account from * cognito * in nodejs.

I'm trying to delete the user from cognito it is not working for me.

AWS config

const AWS = require('aws-sdk'); const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();

Pool config

const poolConfig = { UserPoolId: keys.cognito.userPoolId, ClientId: keys.cognito.clientId };

Above configurations comes on top of below delete function.

Delete Function

function deleteUserFunc(req, decodedToken) {
    return new Promise((resolve, reject) => {
        const decodedEmailid = decodedToken.email;
        const decodedSub = decodedToken.sub;
        try {
                const userDetails = { Username: decodedSub, Pool: userPool };
                console.log('DEBUG : ' + JSON.stringify(userDetails));
                const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userDetails);

                // Attempting to delete the user

                cognitoidentityserviceprovider.adminDeleteUser({
                    UserPoolId: keys.cognito.userPoolId,
                    Username: decodedEmailid
                    }, (err, data)=>                              
                        if(err) {
                        return reject({
                            error: err.message
                        });
                        } else {
                        return resolve({
                            error: null
                        });
                        }
                    }).promise().catch(err=>{
                    return reject({
                        error: err.message
                    });
                    });

                console.log('User deleteion status : ' + result);

                return resolve({
                    error: null,
                });
            });
        } catch (err) {
            return reject({
                error: err,
            });
        }
    });
}

I have also tried to delete the user with const userDetails = { Username: decodedEmailId, Pool: userPool } but no use.

Any help will much appreciated.

like image 900
Rajath Avatar asked May 07 '19 10:05

Rajath


People also ask

How do I delete a Cognito user in AWS?

You can use the main aws javascript SDK and call the adminDeleteUser operation. It is an authenticated operation and it will require developer credentials for you to call it.

Can Cognito username be changed?

The user name is a fixed value that users can't change. If you mark an attribute as an alias, users can sign in with that attribute in place of the user name. You can mark the email address, phone number, and preferred username attributes as aliases.

How do I delete all users on Cognito?

You must have jq installed and remember to make the script executable: chmod +x deleteAllUsers.sh . The user pool id can be provided as a command line argument: ./deleteAllUsers.sh COGNITO_USER_POOL_ID . Thanks, this is perfect.


1 Answers

Try this:

const AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: 'access key id',
  secretAccessKey: 'secret access key',
  region: 'region',
});
const cognito = new AWS.CognitoIdentityServiceProvider();

await cognito.adminDeleteUser({
  UserPoolId: 'pool id',
  Username: 'username',
}).promise();

Note that the access token you're using within sdk should have cognito-idp:AdminDeleteUser permission on required pool.

like image 135
Vladyslav Usenko Avatar answered Nov 10 '22 22:11

Vladyslav Usenko