Reputation: 3735
Logging in to Cognito from a web page works, I get both access token back and id token. Now I want to run a Lambda function upon login and access some data on the user, but here it fails..
I get InvalidLambdaResponseException: Invalid lambda trigger source
.
Any ideas on what's causing this?
The Java Lambda code is just this:
public class LambdaFunctionHandler implements RequestHandler<CognitoEvent, CognitoEvent> {
@Override
public CognitoEvent handleRequest(CognitoEvent event, Context context)
{
context.getLogger().log("Input: " + event);
return event;
}
}
Javascript:
function loginCognito()
{
AWSCognito.config.region = 'us-east-1';
var authenticationData = {
Username : '***',
Password : '***',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1*********',
ClientId : '*******************'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
Username : '***',
Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails,
{
onSuccess: function (result) {
/* ... */
},
onFailure: function(err) {
alert(err);
}
});
}
Upvotes: 1
Views: 2196
Reputation: 5572
Without knowing too much about the specifics of what you are trying to do and based on the error you are getting back I believe that the triggerSource does not have a value in one of the value:
PreSignUp_SignUp, PostConfirmation_ConfirmSignUp, PostConfirmation_ConfirmForgotPassword, PreAuthentication_Authentication, PostAuthentication_Authentication, CustomMessage_SignUp, CustomMessage_AdminCreateUser, CustomMessage_ResendCode, CustomMessage_ForgotPassword, CustomMessage_UpdateUserAttribute, CustomMessage_VerifyUserAttribute, CustomMessage_Authentication, DefineAuthChallenge_Authentication, CreateAuthChallenge_Authentication, VerifyAuthChallengeResponse_Authentication
Now the reason this does not work is that the CognitoEvent is a template (example) for the CognitoSync service and not for the userPools that you use. Currently we do not provide a JAVA example of the input event.
To make it work, you need to have an input object that can serialize the following JSON
{
"version": 1,
"triggerSource": "PostAuthentication_Authentication",
"region": "<region>",
"userPoolId": "<userPoolId>",
"userName": "<userName>",
"callerContext": {
"awsSdk": "<calling aws sdk with version>",
"clientId": "<apps client id>",
...
},
"request": {
"userAttributes": {
"phone_number_verified": true,
"email_verified": true,
... //all custom attributes
}
},
"response": {}
};
Upvotes: 2