I'm developing a lambda function to login into Cognito, but I'm having problems to do it wait for my cognito authentication.
I tried to use the code just like it is in examples, passing a onSuccess and a onFailure function as parameter, but the function is complete without wait. Then, I tried to make a promise, just like I did sending SES email, but it give the message "Cannot read property 'promise' of undefined"
My code until now:
'use strict';
global.fetch = require('node-fetch');
let AmazonCognitoIdentity = require('amazon-cognito-identity-js');
function criarResposta( statusCode, retorno ) {
return {
statusCode: statusCode,
body: retorno
};
}
module.exports.login = async (event) => {
let enviar_promise = null;
let nome_usuario = "::USER_NAME::";
let senha_usuario = "::USER_PASSWORD::";
let authentication_data = {
Username : nome_usuario,
Password : senha_usuario,
};
let authentication_details = new AmazonCognitoIdentity.AuthenticationDetails(authentication_data);
let pool_data = {
UserPoolId : '::USER_POOL_ID::',
ClientId : '::CLIENT_ID::'
};
let user_pool = new AmazonCognitoIdentity.CognitoUserPool(pool_data);
let user_data = {
Username : nome_usuario,
Pool : user_pool
};
let cognito_user = new AmazonCognitoIdentity.CognitoUser(user_data);
enviar_promise = cognito_user.authenticateUser( authentication_details ).promise();
try {
const dados = await enviar_promise;
return criarResposta( 200, `{
"message": "OK"
}` );
} catch (err) {
console.error(err, err.stack);
return criarResposta( 500, `{
"message": "Erro interno"
}` );
}
};
EDIT
I've update my code according to an example and now it looks like it is waiting for response and it returns code 200 and {message: "OK"}, but it prints undefined on console.log( resultado );
Code:
'use strict';
global.fetch = require('node-fetch');
let AmazonCognitoIdentity = require('amazon-cognito-identity-js');
let AWS = require('aws-sdk');
function criarResposta( statusCode, retorno ) {
return {
statusCode: statusCode,
body: retorno
};
}
module.exports.login = async (event) => {
let enviar_promise = null;
let nome_usuario = "::USER_NAME::";
let senha_usuario = "::USER_PASSWORD::";
let authentication_data = {
Username : nome_usuario,
Password : senha_usuario,
};
let authentication_details = new AmazonCognitoIdentity.AuthenticationDetails(authentication_data);
let pool_data = {
UserPoolId : '::USER_POOL_ID::',
ClientId : '::CLIENT_ID::'
};
let user_pool = new AmazonCognitoIdentity.CognitoUserPool(pool_data);
let user_data = {
Username : nome_usuario,
Pool : user_pool
};
let cognito_user = new AmazonCognitoIdentity.CognitoUser(user_data);
try {
let resultado = await cognito_user.authenticateUser( authentication_details );
// let access_token = resultado.getAccessToken().getJwtToken();
// let id_token = resultado.idToken.jwtToken;
console.log( resultado );
return criarResposta( 200, `{
"message": "OK"
}` );
} catch (err) {
console.error(err, err.stack);
return criarResposta( 500, `{
"message": "Erro interno"
}` );
}
};
EDIT 2
So, I made a pure nodejs code and it returns the access token and token id:
global.fetch = require('node-fetch');
let AmazonCognitoIdentity = require('amazon-cognito-identity-js');
let AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: '::VALOR::',
secretAccessKey: '::VALOR::',
region: 'us-east-2'
});
let enviar_promise = null;
let nome_usuario = "::VALOR::";
let senha_usuario = "::VALOR::";
let authentication_data = {
Username : nome_usuario,
Password : senha_usuario,
};
let authentication_details = new AmazonCognitoIdentity.AuthenticationDetails(authentication_data);
let pool_data = {
UserPoolId : '::VALOR::',
ClientId : '::VALOR::'
};
let user_pool = new AmazonCognitoIdentity.CognitoUserPool(pool_data);
let user_data = {
Username : nome_usuario,
Pool : user_pool
};
let cognito_user = new AmazonCognitoIdentity.CognitoUser(user_data);
cognito_user.authenticateUser(authentication_details, {
onSuccess: function (result) {
let accessToken = result.getAccessToken().getJwtToken();
let idToken = result.idToken.jwtToken;
console.log( accessToken );
console.log( idToken );
},
onFailure: function(err) {
console.log(err);
},
});
I think the problem is in try to send authenticateUser as a promise, but I don't know how to make lambda wait my request without it