I'm also working on a front end Typescript application that uses both Authenticated and Unauthenticated identities from Cognito.
For unauthenticated identities, my flow looks like this:
CognitoIdentity.getId()
.That code looks like this:
var cognitoidentity = new AWS.CognitoIdentity();
var params = {
IdentityPoolId: 'us-east-1:bxxxxxx-cxxx-4xxx-8xxx-9xxxxxxxxxxx'
};
// tslint:disable-next-line:no-any
cognitoidentity.getId(params, function(err: any, data: any) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-1:bxxxxxxx-cxxx-4xxx-8xxx-9xxxxxxxxxxx',
IdentityId: data.IdentityId
});
// access AWS resources
}
});
This will result in your app getting temporary IAM credentials (access key, secret key, session token) that are used to access back end resources.
The role assumed when you use these keys will be the role you configured in your Identity Pool settings:
This way you don't have to expose the IAM role name to the browser, either. AWS will simply assume the correct role based on the IAM keys.
Happy Hacking!