如何通过Cognito SDK作为管理员更新用户属性

3
我正在创建一个Angular应用程序,在其中我希望通过AWS Cognito添加身份验证(我对AWS非常陌生)。我已经成功地添加了注册,登录,注销,MFA等功能。另外,我想创建像管理面板之类的东西,管理员可以在其中更改普通用户的属性。但是我不确定如何实现这些管理方面的事情。管理员应该如何登录?管理员应该如何注册?是否有专门为他们设置的用户池?然后作为管理员如何管理(更改属性)普通用户?
我已经阅读过AWS文档,但不够清晰。我看到有一些以Admin为前缀的操作,例如AdminUpdateUserAttributes,但我不确定如何使用它们。
编辑:我尝试了以下内容:
const AWS = require('aws-sdk');
let cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({apiVersion: '2016-04-18'});

let params = {
    UserAttributes: [{
        Name: 'custom:state',
        Value: this.newValue
    }],
    UserPoolId: 'us-east-1_example',
    Username: this.username
};
cognitoIdentityServiceProvider.adminUpdateUserAttributes(params, function(err, data) {
    // do something with result
    err && console.error(err);
    data && console.log(data);
});

但是我遇到了以下错误:CredentialsError:配置中缺少凭据

我应该如何设置这些凭据?

1个回答

0
为了拥有管理员权限,您需要提供accessKeyIdsecretAccessKeyidToken。其中一种方法是从AWS管理控制台获取这些密钥。它们可以从具有修改所需用户池权限的IAM角色中提取。然后您可以执行以下操作:
AWS.config.update({accessKeyId: '...', secretAccessKey: '...'});

我在我的应用程序中所做的是创建另一个用户池来管理管理员。然后,我将此用户池添加为身份提供者到身份池中。接着,我编辑了授权的 IAM 角色以便可以与一般用户一起编辑用户池。
以下方法对我有效:
const userPool = new CognitoUserPool({
  UserPoolId: this.adminUserPoolId,
  ClientId: this.adminClientId
});

const authenticationDetails = new AuthenticationDetails({
  Username: username,
  Password: password
});
const cognitoUser = new CognitoUser({
  Username: username,
  Pool: userPool
});
cognitoUser.authenticateUser(authenticationDetails, ....);

const jwt = cognitoUser.getSignInSession().getIdToken().getJwtToken();
const provider = `cognito-idp.${this.region}.amazonaws.com/${this.adminUserPoolId}`;

AWS.config.update({
  credentials: new CognitoIdentityCredentials({
    IdentityPoolId: this.identityPoolId,
    Logins: {
      [provider]: jwt // when you provide the jwt, accessKeyId and secretAccessKey are extracted
    }
 })
});

const identityService = new CognitoIdentityServiceProvider();
identityService.adminUpdateUserAttributes(...);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接