Reputation: 751
I am running a AWS Cognito signin process authentication API on AWS SAM local. I am getting authed correctly from Cognito but when the signInUser promise resolves (with the correct response), instead of the callback with the statusCode of 200 getting fired, it fires the callback in the catch (with the statusCode of 400).
See Lambda function here:-
// A signin Lambda function
export function handler (event: Object, context: Object, callback: Function) {
switch (event.httpMethod) {
case "GET":
// hard code login for SO question
signInUser({ username: 'XXXX', password: 'XXXXXXX'})
.then((response) => {
console.log('This log is called correctly but callback on the next line is not');
callback(null, {
statusCode: 200,
header: response.tokens.idToken.jwtToken,
body: "This is a signin operation, return success result"
});
})
.catch(
callback(null, {
statusCode: 400,
body: "This is a failed signin operation"
})
);
break;
default:
// Send HTTP 501: Not Implemented
console.log("Error: unsupported HTTP method (" + event.httpMethod + ")");
callback(null, {statusCode: 501})
}
}
Any ideas what is causing this to happen or how to fix it?
Much thanks!
Upvotes: 0
Views: 825
Reputation: 92440
.catch()
takes a function, but you are passing it the result of your callback. Try this:
.catch( (error) =>
callback(null, {
statusCode: 400,
body: "This is a failed signin operation"
})
)
Upvotes: 1