Get Cognito user pool identity in Lambda function

前端 未结 2 641
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 01:52

I have a Lambda function handling POST requests triggered by the API Gateway. The latter is set up to authorize via a Cognito user pool authorizer. Authorization works - i

2条回答
  •  青春惊慌失措
    2020-12-18 02:37

    Use the event.requestContext.authorizer.claims.sub to get user's Cognito identity sub, which is basically their ID. This assumes you're using Proxy Integration with API Gateway and Lambda.

    Here's a simple example using Node; should be similar across other SDKs.

    exports.handler = async (event, context, callback) => {
        let cognitoIdentity = event.requestContext.authorizer.claims.sub
    
        // do something with `cognitoIdentity` here
    
        const response = {
            statusCode: 200,
            headers: {
                "Access-Control-Allow-Origin": "*"
            },        
            body: JSON.stringify("some data for user"),
        };
        return response;
    };
    

提交回复
热议问题