var cognitoIdentity = new AWS.CognitoIdentity(); var lambda = new AWS.Lambda(); function login() { var cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider(); var email = document.getElementById('inputEmail'); var password = document.getElementById('inputPassword'); var params = { AuthFlow: "USER_PASSWORD_AUTH", ClientId: cognitoUserPoolClientId, AuthParameters: { USERNAME: email.value, PASSWORD: password.value } }; cognitoIdentityServiceProvider.initiateAuth(params, function (err, data) { if(err !== null) { // Something has gone wrong. // TODO: Add some error logging, reporting etc. console.log(err); return; } if(data.hasOwnProperty('ChallengeName')) { // There is a challenge to respond to. console.log(data); } else { // There is no challenge and we're logged in. console.log(data); var id; cognitoIdentity.getId({IdentityPoolId: cognitoIdentityPoolId, Logins: {[cognitoIDP]: data.AuthenticationResult.IdToken}},function(err,data){ console.log(err); console.log(data); id = data.IdentityId; }); cognitoIdentity.getCredentialsForIdentity({IdentityId: id}, function(err,data){console.log(err);console.log(data);}); } }); } var form = document.getElementById('login-form'); form.addEventListener('submit', function (evt) { evt.preventDefault(); login(); });