【问题标题】:How to update user attribute as an admin via the Cognito SDK如何通过 Cognito SDK 以管理员身份更新用户属性
【发布时间】:2020-02-16 03:39:01
【问题描述】:

我正在创建一个 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: Missing credentials in config

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

【问题讨论】:

    标签: javascript angular aws-sdk amazon-cognito


    【解决方案1】:

    为了拥有admin 权限,您需要提供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(...);
    
    

    【讨论】:

      猜你喜欢
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多