Reputation: 239
currently I am trying to refresh a cognito user session. There are alot of examples in the internet, but how I look into cognito is a little bit different.
I don't login via username and password... I login with the accessToken, idToken and refreshToken.
Let me show you, how I do that:
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();
}
});
});
This works without any problems. But after 30 Minutes, I need to refresh the tokens, because they expired.
I do that with this code:
cognitoUser.refreshSession(cognitoUser.getSignInUserSession().getRefreshToken(), (error) => {
if(error) {
console.error(error);
} else {
console.info('Refresh logged in session.');
}
});
But this call gives me an error, that the refresh token is invalid.
POST https://cognito-idp.eu-central-1.amazonaws.com/ 400
{code: "NotAuthorizedException", name: "NotAuthorizedException", message: "Invalid Refresh Token"}
The body of the call above
{
"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"
}
}
Does anyone have an Idea, how do I can solve that?
Thanks for your time!
Upvotes: 1
Views: 2065
Reputation: 3729
Can You Please try with below code :
reference link : 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);
};
Upvotes: 1