Problem Refreshing The Aws Cognito Id Token
I'm trying to refresh the AWS Cognito ID Token using the AWS SDK for javascript. We need the token ID to be refreshed automatically without any action with our users. I create the
Solution 1:
The problem is solved by using the following statement instead of using AWS.config.Credentials.refresh
:
( < AWS.CognitoIdentityCredentials > myAwsConfig.credentials).refresh();
Here is the completed code that works and it refreshes the token ID of the AWS Cognito User:
refreshToken(success, failure) {
var poolData = {
UserPoolId: this.initializeData.UserPoolId,
ClientId: this.initializeData.ClientId,
};
var userPool = newCognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();
var currentSession = null;
cognitoUser.getSession(function (err, session) {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
currentSession = session;
});
if(!currentSession){
failure("there is a problem with current session. try again later.");
}
AWS.config.region = 'us-east-2';
AWS.config.credentials = newAWS.CognitoIdentityCredentials({
IdentityPoolId: this.initializeData.IdentityPoolId,
Logins: {
'cognito-idp.us-east-2.amazonaws.com/poolId': currentSession.getIdToken().getJwtToken(),
},
});
var refresh_token = currentSession.getRefreshToken();
cognitoUser.refreshSession(refresh_token, (err, session) => {
if (err) {
failure(err);
} else {
var myAwsConfig = AWS.config;
myAwsConfig.credentials.sessionToken = session.getIdToken().getJwtToken();
( < AWS.CognitoIdentityCredentials > myAwsConfig.credentials).refresh(err => {
if (err) {
failure(err);
} else {
success(session.getIdToken().getJwtToken());
}
});
}
});
}
Post a Comment for "Problem Refreshing The Aws Cognito Id Token"