-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What's the best practise to refresh Cognito tokens? #2560
Comments
@yunganw Regarding the network traffic, I believe getUserData will use cached data unless your pass in a bypassCache parameter. Are you having some sort of other issue with the tokens not refreshing? |
Correct. Latest version has this param. Thanks. |
Here is what I learned after working on two projects.
|
@mzohaibqc I like your answer -- it got me where I needed to go, so thank you. Just a thought though, and it may not really be important but... As the docs say and I'm sure you know,
|
@jsheebs104 Thanks for sharing this. Small improvements matter. I don't know about the impact of using |
@mzohaibqc Hi a question re your proposed solution, is there a reason why you don't extract the try {
const cognitoUser = await Auth.currentAuthenticatedUser();
const currentSession = cognitoUser.signInUserSession;
cognitoUser.refreshSession(currentSession.refreshToken, (err, session) => {
// do something with the new session
});
} catch (e) {
// whatever
}
}; |
@giulioambrogi I can't test this but seems like a good point and will save one refresh session api call if your session is expired at the time of execution. |
I am using Auth.currentSession();. But sometime it failed to give the token I am getting 401: {message:'The incoming token has expired'} Then how the token is automatically refreshing here?
|
This will be helpful for anyone willing to refresh Id Token
|
Thanks @mzohaibqc for your answer! I have one further issue. At which point in a react application should I trigger the refresh of the token? Is it a good idea to do it in the root component like in app.js or where in a react application should I do this? |
@nklswbr You can refresh token in App.js or you can create separate background task like using redux-saga which will refresh your token on application load time or you can create a saga to keep refreshing this after every 50 minutes or so. |
We're having a problem tho that is the refreshSession is taking too much time (around 10 seconds) specifically when the app is being stale for a while (if you leave it for 5 to 7 minutes for example and then come back and refresh the page). any idea why is that ? |
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs. Looking for a help forum? We recommend joining the Amplify Community Discord server |
** Which Category is your question related to? **
Using User Pool as APIGW's authorizor.
It shall pass the Cognito IdToken in the 'Authorization' header of each API request.
This idToken will expire every hour after granted.
The SDK document says Amplify will automatically update/refresh the tokens.
How to refresh Cognito tokens only when necessary?
What's the suggested code to refresh tokens? More detailed questions in the code snippets part
** What AWS Services are you utilizing? **
Cognito User Pool and APIGateway
** Provide additional details e.g. code snippets **
Through the following issue said to call Auth.currentSession() to refresh token is the right code, there is some additional unnecessary network call in that process.
#446
When the APIGW request called frequently, and every time before sending the request, to call currentSession with this unnecessary network access causes latencies.
Using Auth.currentSession() will call getUserData every time, which causes unnecessary network request/response. How to make a correct way to have a valid token for each API request and the refresh token operation is only done when necessary and there is no additional network access when refreshing the tokens?
https://aws-amplify.github.io/docs/js/authentication#retrieve-current-session
https://github.com/aws-amplify/amplify-js/blob/master/packages/auth/src/Auth.ts#L998
https://github.com/aws-amplify/amplify-js/blob/master/packages/auth/src/Auth.ts#L905
The text was updated successfully, but these errors were encountered: