'AWS cognito: adminUpdateUserAttributes not working and not giving an error , am i missing something?
I can't get adminUpdateUserAttributes for Cognito to work. The cli works and I can have the user add/changed them not desired but wanted to see it working.
I'm using the AmazonCognitoPowerUser an AWS managed policy on the lambda function and the lambda is triggering, is there something I'm missing this sounds and looks easy but it's just not working.
also is there a way to get the default Created date without making my own.
const AWS = require('aws-sdk');
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
exports.handler = async (event) => {
cognitoidentityserviceprovider.adminUpdateUserAttributes(
{
UserAttributes: [
{
Name: 'custom:Date_Created',
Value: new Date().toString()
}
....
],
UserPoolId: " the correctpool id",
Username: "dagTest"
},
function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data);
}
)};
// no errors and returns nothing as it says it should.
Solution 1:[1]
I guess it is because you are not waiting for the result and the lambda is terminating after adminUpdateUserAttributes()
is called and dows not wait until it is returning.
I would suggest that you change to promise based calling and do a try/catch
exports.handler = async (event) => {
try{
// no callback here
const data = await cognitoidentityserviceprovider
.adminUpdateUserAttributes(attributes)
.promise()
console.log('success', data)
} catch(error) {
console.error('error', error)
}
)}
Solution 2:[2]
@thopaw The code is correct but didn't work for me as intended. I was still getting Auth Error in the front end even through the custom attributes were updated successfully in the Cognito AWS Console. I had to add context.done(null,event);
in order to return the control to Cognito after the execution of lambda. So the updated one could be,
exports.handler = async (event, context) => {
try{
const data = await cognitoidentityserviceprovider
.adminUpdateUserAttributes(attributes)
.promise()
console.log('success', data)
} catch(error) {
console.error('error', error)
}
context.done(null,event);
)}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | thopaw |
Solution 2 | Abinash |