是否可以在Lambda触发器中修改AWS Cognito用户属性 [英] Is it possible to modify AWS Cognito user attributes in the Lambda triggers

查看:192
本文介绍了是否可以在Lambda触发器中修改AWS Cognito用户属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅AWS文档

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

您可以在 预注册 Lambda函数中使用以下参数:

you have the following paramaters available in the Pre Sign-up Lambda fuction:

"request": {
  "userAttributes": {
    "string": "string",
    ....
},
"validationData": {<validation data as key-value (String, String) pairs, from the client>}

是否可以修改或添加事件对象的其他 userAttributes

is there a way to modify or add additional userAttributes the the event object?

例如:

// Modify an existing username...
event.request.userAttributes.name.ucfirst();

// Add an additional attribute...
event.request.userAttributes.nickname = "ANY_NAME";


callback(null, event);


推荐答案

是的,绝对有办法!您需要在Lambda处理程序中使用AWS javascript SDK:

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) {
    ...
  }
);

确保为Lambda函数提供正确的策略(即允许 cognito-idp:AdminUpdateUserAttributes操作),并且用户池已定义了属性。

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.

这篇关于是否可以在Lambda触发器中修改AWS Cognito用户属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆