【问题标题】:Fetching Cognito user's device attributes in lambda function在 lambda 函数中获取 Cognito 用户的设备属性
【发布时间】:2021-09-09 13:43:59
【问题描述】:

我创建了一个 lambda 函数,该函数在 Cognito 用户池身份验证触发器的 Post-Confirmation 中触发。这个想法是在用户成功通过身份验证后,将用户 ID、电话号码和用户设备密钥(以及所有其他设备属性)放入我的 dynamodb 用户表中。 我的用户设备属性已在 Cognito 中成功捕获,我可以在每个用户下看到它们。

我能够获取 userId 和 phone_number 但我无法在 lambda 函数中获取用户设备属性。请帮忙。

下面是lambda函数的代码。

var aws = require('aws-sdk');
var ddb = new aws.DynamoDB({apiVersion: '2012-10-08'});


exports.handler = async (event, context) => {
    console.log(event);

    let date = new Date();

    const tableName = "user_table";
    const region = process.env.REGION;
    
    console.log("table=" + tableName + " -- region=" + region);

    aws.config.update({region: region});

    if (event.request.userAttributes.sub) {
        
        console.log("Event Requests", event.request);
        let ddbParams = {
            Item: {
                'id': {S: event.request.userAttributes.sub},
                'phone_number': {S: event.request.userAttributes.phone_number},
                'device_key' :    **//Need to fetch DeviceKey of the user. Solution required.**
            },
            TableName: tableName
        };

        try {
            await ddb.putItem(ddbParams).promise()
            console.log("Success");
        } catch (err) {
            console.log("Error", err);
        }

        console.log("Success: Everything executed correctly");
        context.done(null, event);

    } else {
        console.log("Error: Nothing was written to DDB or SQS");
        context.done(null, event);
    }
};

【问题讨论】:

    标签: aws-lambda amazon-cognito aws-amplify amazon-dynamodb


    【解决方案1】:

    device_key(或任何其他设备属性)不属于Cognito standard attributes。为了访问它,您需要在名称前加上 custom::

    // ...
    device_key: {S: event.request.userAttributes['custom:device_key']},
    // ...
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 2021-12-10
      • 2015-11-26
      • 2023-02-13
      • 2019-01-01
      • 2018-07-17
      相关资源
      最近更新 更多