Reputation: 1174
If a user logs in, I'm checking if the user has the required policies for IoT, if not, i'm attaching it.
This works fine, if i'm logging in for the first time.
Now when I'm logging out, and try to login with a different user, for some reason the credentials are missing, and when i'm refreshing the page, its working again....
window.login = function() {
var shadowsRegistered = false;
AWSCognito.config.region = AWSConfiguration.region;
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: AWSConfiguration.IdPoolId
});
var authenticationData = {
Username : document.getElementById("benutzername").value,
Password : document.getElementById("passwort").value
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId : AWSConfiguration.UserPoolId,
ClientId : AWSConfiguration.ClientAppId
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : document.getElementById("benutzername").value,
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
AWS.config.region = AWSConfiguration.region;
var auth_params = {
IdentityPoolId: AWSConfiguration.IdPoolId,
Logins : {
'cognito-idp.eu-central-1.amazonaws.com/eu-central-XXXX' : result.getIdToken().getJwtToken()
}
};
AWS.config.credentials = new AWS.CognitoIdentityCredentials(auth_params);
var cognitoIdentity = new AWS.CognitoIdentity();
cognitoIdentity.getId(auth_params, function(err, data) {
if (err) {
cognitoId = AWS.config.credentials.identityId;
}
else{
cognitoId = data.IdentityId;
}
var iot = new AWS.Iot();
iot.listPrincipalPolicies({principal: cognitoId}, function(err, data) {
if (err) {
console.log(err, err.stack); //ERROR on 2nd login
}
else{
// not related, works on the first login..
The Error I'm receiving:
CredentialsError: Missing credentials in config
Upvotes: 1
Views: 1187
Reputation: 1174
I fixed it by myself. You need to clear the cached credentials.
$('#logout').click(function() {
currentUser = userPool.getCurrentUser();
currentUser.signOut();
AWS.config.credentials.clearCachedId();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({});
location.reload();
});
Upvotes: 2