要在不登录用户的情况下更新AWS Cognito用户的自定义属性,您可以使用AWS Cognito的管理API来执行此操作。以下是一个使用AWS SDK for JavaScript(例如Node.js)的示例代码:
const AWS = require('aws-sdk');
// 配置AWS SDK
AWS.config.update({
region: 'your-aws-region',
accessKeyId: 'your-access-key',
secretAccessKey: 'your-secret-access-key'
});
// 创建CognitoIdentityServiceProvider实例
const cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();
// 更新用户自定义属性的函数
const updateUserAttributes = async (userPoolId, username, attributes) => {
const params = {
UserPoolId: userPoolId,
Username: username,
UserAttributes: attributes
};
try {
const response = await cognitoIdentityServiceProvider.adminUpdateUserAttributes(params).promise();
console.log('用户属性已更新:', response);
} catch (error) {
console.error('更新用户属性时发生错误:', error);
}
};
// 调用更新用户自定义属性的函数
const userPoolId = 'your-user-pool-id';
const username = 'user-to-update';
const attributes = [
{
Name: 'customAttribute1',
Value: 'new-value1'
},
{
Name: 'customAttribute2',
Value: 'new-value2'
}
];
updateUserAttributes(userPoolId, username, attributes);
请确保将示例代码中的your-aws-region,your-access-key,your-secret-access-key和your-user-pool-id替换为相应的值。另外,将user-to-update替换为要更新属性的特定用户的用户名,并根据需要添加或删除attributes数组中的自定义属性。