I'm new to TypeScript (and JavaScript) and I'm having some issues to wait until a function from an external library, namely amazon-cognito-identity-js is executed. I want to first authenticate an user and then get a token from that user. That's it. I read this nice tutorial about Promises and there is also an example on the official page of the library, but seems like I'm missing something.
I created the following code:
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool
} from "amazon-cognito-identity-js";
var DEV_USER_POOL_DATA = {
UserPoolId: 'userPool',
ClientId: 'clientId'
};
var USER_POOL_DATA = DEV_USER_POOL_DATA;
function getJWT() {
return new Promise<void>((resolve) => {
resolve();
});
}
function login() {
let userPool = new CognitoUserPool(USER_POOL_DATA);
let authenticationDetails = new AuthenticationDetails({
Username: "myUser",
Password: "myPass",
});
let cognitoUser = new CognitoUser({ Username: "myUser", Pool: userPool });
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function(result) {
return result.getAccessToken().getJwtToken();
},
onFailure: function(err) {
console.log(err);
}
});
}
getJWT().then(login);
console.log('Hello world');
However when I run the code I see
Hello world
<The actual token I will like to see first>
Do you know what is wrong?