刷新 AWS Cognito ID 令牌时出现问题
Problem refreshing the AWS Cognito ID Token
我正在尝试使用 javascript 的 AWS 开发工具包刷新 AWS Cognito ID 令牌。我们需要自动刷新令牌 ID,而无需我们的用户采取任何操作。我创建了以下函数,我们将检查身份验证后获取的过期时间,当当前时间接近过期时间时,我们将调用此函数。
refreshToken(success, failure) {
var poolData = {
UserPoolId: this.initializeData.UserPoolId,
ClientId: this.initializeData.ClientId,
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();
cognitoUser.getSession(function (err, session) {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
AWS.config.region = 'us-east-2';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'myIdentityPoolId',
Logins: {
'cognito-idp.us-east-2.amazonaws.com/myPoolId': session.getIdToken().getJwtToken(),
},
});
AWS.config.credentials.refresh(error => {
if (error) {
console.error(error);
} else {
var refresh_token = session.getRefreshToken();
console.log('refresh started.');
cognitoUser.refreshSession(refresh_token, (err, session) => {
if (err) {
console.log(err);
} else {
AWS.config.credentials.params.Logins[
'cognito-idp.us-east-2.amazonaws.com/myPoolId'
] = session.getIdToken().getJwtToken();
AWS.config.Credentials.refresh(err => {
if (err) {
console.log(err);
} else {
console.log('TOKEN SUCCESSFULLY UPDATED');
console.log(cognitoUser.getSession().getJWTToken);
}
});
}
});
}
});
});
}
但是我在我的控制台中收到以下错误。
TypeError: AWS.config.Credentials is undefined
在以下行中:
AWS.config.Credentials.refresh(err => {
我应该怎么做才能解决这个问题?
使用下面的语句代替AWS.config.Credentials.refresh
解决了问题:
( < AWS.CognitoIdentityCredentials > myAwsConfig.credentials).refresh();
这是完整的有效代码,它刷新了 AWS Cognito 用户的令牌 ID:
refreshToken(success, failure) {
var poolData = {
UserPoolId: this.initializeData.UserPoolId,
ClientId: this.initializeData.ClientId,
};
var userPool = new CognitoUserPool(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 = new AWS.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());
}
});
}
});
}
我正在尝试使用 javascript 的 AWS 开发工具包刷新 AWS Cognito ID 令牌。我们需要自动刷新令牌 ID,而无需我们的用户采取任何操作。我创建了以下函数,我们将检查身份验证后获取的过期时间,当当前时间接近过期时间时,我们将调用此函数。
refreshToken(success, failure) {
var poolData = {
UserPoolId: this.initializeData.UserPoolId,
ClientId: this.initializeData.ClientId,
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var cognitoUser = userPool.getCurrentUser();
cognitoUser.getSession(function (err, session) {
if (err) {
alert(err.message || JSON.stringify(err));
return;
}
AWS.config.region = 'us-east-2';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'myIdentityPoolId',
Logins: {
'cognito-idp.us-east-2.amazonaws.com/myPoolId': session.getIdToken().getJwtToken(),
},
});
AWS.config.credentials.refresh(error => {
if (error) {
console.error(error);
} else {
var refresh_token = session.getRefreshToken();
console.log('refresh started.');
cognitoUser.refreshSession(refresh_token, (err, session) => {
if (err) {
console.log(err);
} else {
AWS.config.credentials.params.Logins[
'cognito-idp.us-east-2.amazonaws.com/myPoolId'
] = session.getIdToken().getJwtToken();
AWS.config.Credentials.refresh(err => {
if (err) {
console.log(err);
} else {
console.log('TOKEN SUCCESSFULLY UPDATED');
console.log(cognitoUser.getSession().getJWTToken);
}
});
}
});
}
});
});
}
但是我在我的控制台中收到以下错误。
TypeError: AWS.config.Credentials is undefined
在以下行中:
AWS.config.Credentials.refresh(err => {
我应该怎么做才能解决这个问题?
使用下面的语句代替AWS.config.Credentials.refresh
解决了问题:
( < AWS.CognitoIdentityCredentials > myAwsConfig.credentials).refresh();
这是完整的有效代码,它刷新了 AWS Cognito 用户的令牌 ID:
refreshToken(success, failure) {
var poolData = {
UserPoolId: this.initializeData.UserPoolId,
ClientId: this.initializeData.ClientId,
};
var userPool = new CognitoUserPool(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 = new AWS.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());
}
});
}
});
}