目前,我正在尝试刷新一个认知用户会话。互联网上有很多例子,但我对认知的看法有点不同。
我不通过用户名和密码登录..。我用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"
}
}有没有人知道我该怎么解决这个问题?
耽误您时间,实在对不起!
发布于 2018-10-29 10:14:01
请您试一下下面的代码:
参考链接:https://gist.github.com/kndt84/5be8e86a15468ed1c8fc3699429003ad
cognitoUser = getCognitoUser(req);
cognitoUser.refreshSession(RefreshToken, (err, session) => {
if (err) throw err;
//get token code
});
getCognitoUser = function(req) {
const poolData = {
UserPoolId : COGNITO_USER_POOL_ID,
ClientId : COGNITO_CLIENT_ID
};
const userPool = new CognitoUserPool(poolData);
const userData = {
Username : req.user.email,
Pool : userPool
};
return new CognitoUser(userData);
};https://stackoverflow.com/questions/52989995
复制相似问题