Serverless-stack-com: Comments: Clear AWS Credentials Cache

Created on 10 Apr 2017  路  14Comments  路  Source: AnomalyInnovations/serverless-stack-com

Most helpful comment

Adding the new AWS.CognitoIdentityCredentials({}) seems to have fixed the issue!

All 14 comments

Hi there, per my comment in https://github.com/AnomalyInnovations/serverless-stack-com/issues/55#issuecomment-318859903 I am still able to see the same "notes list" across users.

Here's an example of me creating a note in one user and seeing it in the other:

I have followed the instructions in the chapter exactly and am still encountering this issue... could there be a problem in code _outside_ of app.js?

After I log out, my localstorage is cleared of any references to Cognito:
image
Logging into a different user gives me a completely difference access token:
image

My package.json states that I am on aws-sdk version "^2.88.0"... Maybe it's a problem with my API's GET handler? list.js:

import * as dynamoDbLib from './libs/dynamodb-lib';
import { success, failure } from './libs/response-lib';

export async function main(event, context, callback) {
    const params = {
        TableName: 'notes',
        // 'KeyConditionExpression' defines the condition for the query
        // - 'userId = :userId': only return items with matching 'userId' partition key
        // 'ExpressionAttributeValues' defines the value in the condition
        // - ':userId': defines 'userId' to bthe User Pool sub of the authenticated user
        KeyConditionExpression: "userId = :userId",
        ExpressionAttributeValues: {
            ":userId": event.requestContext.identity.cognitoIdentityId,
        }
    };

    try {
        const result = await dynamoDbLib.call('query', params);
        // return the matching list of items in response body
        callback(null, success(result.Items));
    }
    catch(e) {
        callback(null, failure({status: false}));
    }
};

@d3sandoval Do you still see the old user's notes after you refresh the page or is it only happening right after you logout and login?

@jayair no. refreshing fixes the issue. Is there an elegant way to get this functionality to work without refreshing? If not, do you know of a good place in the app to force the user to refresh?

@d3sandoval I think we broke it with the latest update. The simplest fix right now would be to change this line https://github.com/AnomalyInnovations/serverless-stack-demo-client/blob/master/src/App.js#L87

AWS.config.credentials.clearCachedId();

to this

delete AWS.config.credentials;

I'll need to test it a bit more and update the tutorial.

@jayair that's worse :/ I get an error message after the login loop... refreshing still fixes it:

For now, I'll just add a refresh on logout... It doesn't look too bad! I just added window.location.reload() after the this.props.history.push('/login'); in handleLogout

@d3sandoval I see. Yeah I'll have to take a look and put in a better fix then.

@d3sandoval I haven't had a chance to update the tutorial yet but if you are still looking for a fix. Try replacing this line - https://github.com/AnomalyInnovations/serverless-stack-demo-client/blob/master/src/App.js#L87

with this.

AWS.config.credentials.clearCachedId();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ });

Adding the new AWS.CognitoIdentityCredentials({}) seems to have fixed the issue!

A related problem occurs if you first attempt to login to a user that doesn't exist and get an
UserNotFound Exception, and then login to a valid user and attempt to create a new note with an attachment which will generate an AccessDenied error, because it was using the wrong access credentials which hadn't expired yet and didn't have a valid identityId defined which also causes it to attempt to create the S3 file: "undefined-1504037928967-filename.ext".

Resetting the AWS.config.credentials as described above fixed this issue as well.

@gregt590 Ah thats true. The update with resetting the credentials is coming out shortly.

Can we close this?

@QuantumInformation You mean this issue? It's the comments thread for this chapter.

I'm not sure if you are referring to the clearing credentials issue. But I think I should mention that this issue has been fixed in this update - https://github.com/AnomalyInnovations/serverless-stack-com/releases/tag/v1.1.

oops, sorry )

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jayair picture jayair  路  14Comments

jayair picture jayair  路  11Comments

Abdizriel picture Abdizriel  路  13Comments

TinCharMan picture TinCharMan  路  12Comments

quantuminformation picture quantuminformation  路  6Comments