当前,我正在尝试刷新认知用户会话。 互联网上有很多示例,但是我对cognito的看法有点不同。
我不通过用户名和密码登录...我使用accessToken,idToken和refreshToken登录。
让我告诉你我该怎么做:
return new Promise(async (resolve, reject) => {
const sessionData = {
IdToken: new CognitoIdToken({IdToken: tokens.idToken}),
AccessToken: new CognitoAccessToken({AccessToken: tokens.accessToken}),
RefreshToken: new CognitoRefreshToken({RefreshToken: tokens.refreshToken})
};
const userSession = new CognitoUserSession(sessionData);
const userData = {
Username: tokens.username,
Pool: this.userPool
};
this.cognitoUser = new CognitoUser(userData);
this.cognitoUser.setSignInUserSession(userSession);
// set the new tokens in the store
const key = `CognitoIdentityServiceProvider.${SETTINGS[stage].ClientId}.${tokens.username}`;
if(tokens.deviceKey) {
localStorage.setItem(`${key}.deviceKey`, tokens.deviceKey);
}
if(tokens.deviceGroupKey) {
localStorage.setItem(`${key}.deviceGroupKey`, tokens.deviceGroupKey);
}
this.cognitoUser!.getSession((error: Error, session: CognitoUserSession) => {
if (session.isValid()) {
resolve();
} else {
reject();
}
});
});
这有效,没有任何问题。但是30分钟后,我需要刷新令牌,因为它们已过期。
我使用以下代码进行操作:
cognitoUser.refreshSession(cognitoUser.getSignInUserSession().getRefreshToken(), (error) => {
if(error) {
console.error(error);
} else {
console.info('Refresh logged in session.');
}
});
但是此调用给我一个错误,表明刷新令牌无效。
POST https://cognito-idp.eu-central-1.amazonaws.com/ 400
{code: "NotAuthorizedException", name: "NotAuthorizedException", message: "Invalid Refresh Token"}
上面的通话内容
{
"ClientId": "4gql86evdegfa...",
"AuthFlow": "REFRESH_TOKEN_AUTH",
"AuthParameters": {
"REFRESH_TOKEN": "eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAifQ.CPZ8hXIXdka7veUdmNY15Zy_FNJ-5SNgTeKmSoBAVNlz_ilcmvBAvluLO3EeUTqOvG-gLSjwzh6TNlz2p18fWjWEfROjr8qby0V3DB_pzO2_cdMXowIhEmKiZ460kJAQBPDQ9EOBs2oJokX-fBVtL0OVIEQYp7NudyARILH3Phrx1BQz3ASLRwX44mlUOa_BkjBQwPkbgqsX7yU2ekJwL5RPllkPql0DitbLEOwZhoTCsnnLJda-rN-uN-0Vf6Q6ZcdZP2QTA6TLhS_Srio7uETtS3YYsZ8-oGIDIPEs4LjtTZQVOJVyBOCRl6...",
"DEVICE_KEY": "eu-central-1_b428daea-9cb4-443d-bbb8-466d8642e4a1"
}
}
有人有想法吗,我该如何解决?
感谢您的时间!
答案 0 :(得分:0)
可以请您尝试以下代码:
参考链接:https://gist.github.com/kndt84/5be8e86a15468ed1c8fc3699429003ad
HORIZONTAL ELLIPSIS