I use AWS Cognito service for authentication. In my Angular 7 app, I use Amplify Auth to guard my pages.
If user navigates between different pages, Amplify will automatically handle the token refresh and they will not see token expirations.
If user stay in one page for long time, then the token will not be refreshed and eventually user will see expired token and will got 403 for web service call.
Any good solution to refresh access/id tokens if user stay in the same page for long time?
Probably two ways :
import { Auth } from 'aws-amplify';
try {
const cognitoUser = await Auth.currentAuthenticatedUser();
const currentSession = await Auth.currentSession();
cognitoUser.refreshSession(currentSession.refreshToken, (err, session) => {
console.log('session', err, session);
const { idToken, refreshToken, accessToken } = session;
// do whatever you want to do now :)
});
} catch (e) {
console.log('Unable to refresh Token', e);
}
more here : https://github.com/aws-amplify/amplify-js/issues/2560
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With