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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With