Clear AWS Credentials Cache
To be able to upload our files to S3 we needed to get the AWS credentials first. And the AWS JS SDK saves those credentials in our browser’s Local Storage.
But we need to make sure that we clear out those credentials when we logout. If we don’t, the next user that logs in on the same browser, might end up with the incorrect credentials.
To do that let’s add the following lines to the handleLogout
method in our src/App.js
above the this.updateUserToken(null);
line.
if (AWS.config.credentials) {
AWS.config.credentials.clearCachedId();
}
So our handleLogout
as a result should now look like so:
handleLogout = (event) => {
const currentUser = this.getCurrentUser();
if (currentUser !== null) {
currentUser.signOut();
}
if (AWS.config.credentials) {
AWS.config.credentials.clearCachedId();
}
this.updateUserToken(null);
this.props.history.push('/login');
}
And include the AWS SDK in the header.
import AWS from 'aws-sdk';
Next up we are going to allow users to see a list of the notes they’ve created.
If you liked this post, please subscribe to our newsletter and give us a star on GitHub.
For help and discussion
Comments on this chapterFor reference, here is the code so far
Frontend Source :clear-aws-credentials-cache