Is it possible to modify AWS Cognito user attributes in the Lambda triggers

前端 未结 4 1487
说谎
说谎 2021-02-18 14:58

Having a look at the AWS documentation,

https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#cogni

4条回答
  •  耶瑟儿~
    2021-02-18 16:04

    Yes, there's absolutely a way! You need to use AWS javascript SDK in your Lambda handler:

    const AWS = require('aws-sdk');
    AWS.config.update({region: 'ap-southeast-1'});
    
    const cognitoidentityserviceprovider =
      new AWS.CognitoIdentityServiceProvider({
        apiVersion: '2016-04-18'
      });
    cognitoidentityserviceprovider.adminUpdateUserAttributes(
      {
        UserAttributes: [
          {
            Name: 'YOUR_USER_ATTRIBUTE_NAME',
            Value: 'YOUR_USER_ATTRIBUTE_VALUE'
          }
        ],
        UserPoolId: event.userPoolId,
        Username: event.userName
      },
      function(err, data) {
        ...
      }
    );
    

    Make sure to give your Lambda function the right policies (i.e. allows "cognito-idp:AdminUpdateUserAttributes" action) and the user pool has the attribute defined.

提交回复
热议问题