Reputation: 389
After the account, which is signed up in Mobile app, verified, i want Lambda function to receive UserID. Due to this reason, i have created POST API in API Gateway. "Invoke with caller credentials" is checked, and Body Mapping Template was added as
{ "identity": "$input.params('identity')" }
Unfortunately, when i have tested it in API Gateway, it gives HTTP Error 404-Not Found Error.
print context.identity
resulted as
<_main__.CognitoIdentity object at 0x7eg6f79v2bed2>
&
print context.identity.cognito_identity_id
resulted as
None
Even, when i have tried from Mobile app, only after i have entered the verification number, HTTP Error 404-Not Found alert is seen in the screen. I have given all permissions as AWSLambdaFullAccess Policy and AmazonAPIGatewayInvokeFullAccess Policy, to Cognito (Auth and UnAuth) IAM Roles. I will be glad if you let me know about the solution of issue.
Lamda Error, API Gateway PUSH API Seetings, API Gateway PUSH API Method
Upvotes: 5
Views: 5497
Reputation: 85
If you use COGNITO_USER_POOLS for the Authorization on that endpoint then you can set the 'Authorization' header as the idToken in the post request. API gateway then verifies the token and passes it through to the function.
Then in your lambda function you should be able to access the Cognito user object like this.
const cognitoUser = event.requestContext.authorizer.claims;
I prefer this to the @Kannaiyan's answer because you don't have to make another call to get the user data.
Upvotes: 3
Reputation: 13055
If you have an authenticated user then that means you have access token for that authentication passed to lambda,
With Access Token
you can obtain user information and its attributes.
var params = {
AccessToken: 'STRING_VALUE' /* required */
};
cognitoidentityserviceprovider.getUser(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
This code is good in node or javascript.
If you are using other SDK's it will have similar signature.
Hope it helps.
Upvotes: 0