Skip to content
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

When auth token is invalid reset cart and refetch details #2379

Merged
merged 8 commits into from
May 20, 2020

Conversation

sirugh
Copy link
Contributor

@sirugh sirugh commented May 4, 2020

Description

The retry logic was not functioning when a guest user tried to perform an operation on an "authed" cart. This situation could arise if a user signs in and then the auth token expires naturally (1 hour ttl). The old, authed, cart id persisted and was used but since there was no more token in store the request failed.

Related Issue

Closes #2378 .

Acceptance

Verification Stakeholders

Specification

Verification Steps

  1. Log in and add an item to your cart.
  2. In local storage, change the M2_VENIA_BROWSER_PERSISTENCE__signin_token entry to have a timeStored of some low number like 1.
  3. Add another item to your cart. The operation should succeed but the cart should only have the item you just added. The cart trigger should be updated to show 1 item in the cart.

Rerun the above steps but on step 3 refresh the page and then add an item.

Screenshots / Screen Captures (if appropriate)

Checklist

  • I have updated the documentation accordingly, if necessary.
  • I have added tests to cover my changes, if necessary.

@m2-community-project m2-community-project bot added this to Ready for Review in Pull Request Progress May 4, 2020
@PWAStudioBot
Copy link
Contributor

PWAStudioBot commented May 4, 2020

Messages
📖

Access a deployed version of this PR here. Make sure to wait for the "pwa-pull-request-deploy" job to complete.

📖 DangerCI Failures related to missing labels/description/linked issues/etc will persist until the next push or next nightly build run (assuming they are fixed).

Generated by 🚫 dangerJS against 4778e15

@sirugh sirugh added the version: Patch This changeset includes backwards compatible bug fixes. label May 4, 2020
@sirugh sirugh requested review from tjwiebell and jimbo May 4, 2020 16:32
err =>
err.message.includes('Could not find a cart') ||
err.message.includes(
'The current user cannot perform operations on cart'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition is all that is necessary for the user flow to "work". The other changes in this PR are to make the UI (cart trigger item qty) update properly without having to refresh the page or add another item

@m2-community-project m2-community-project bot moved this from Ready for Review to Review in Progress in Pull Request Progress May 4, 2020
const [, { toggleDrawer }] = useAppContext();
const [{ cartId }, { getCartDetails }] = useCartContext();

const [getItemCount, { data }] = useLazyQuery(getItemCountQuery);
const [getItemCount, { data }] = useLazyQuery(getItemCountQuery, {
fetchPolicy: 'cache-and-network'
Copy link
Contributor

@tjwiebell tjwiebell May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sirugh - I couldn't get the CartTrigger to update without a refreshing unless I added this fetch policy. I suspect because the manual cache deletions don't trigger lifecycle events, it was still pulling the value from the in-memory cache.

@tjwiebell tjwiebell marked this pull request as ready for review May 13, 2020 21:46
Copy link
Contributor

@supernova-at supernova-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good.

Running through the verification steps though,

  1. Log in and add an item to your cart.
  2. In local storage, change the M2_VENIA_BROWSER_PERSISTENCE__signin_token entry to have a > timeStored of some low number like 1.
  3. Add another item to your cart. The operation should succeed but the cart should only have the > item you just added. The cart trigger should be updated to show 1 item in the cart.

Then I sign out and sign back in again. The item I added in step 1 is in my cart, not the item I added in step 3. This doesn't seem correct.

Edit: Well, I guess that does make sense.
But it's confusing to a user (in this case me) that my authed cart got swapped out for a guest cart. When I add that second item I have no idea that my auth expired. Everywhere else in the app (left drawer) says I'm signed in (authed).

await dispatch(signOut());
} else {
// Delete the cached ID from local storage.
await dispatch(removeCart());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little surprised to see this as an else. I'm assuming both removeCart and signOut delete the auth token from local storage?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok nevermind, just read user/asyncActions. Might be useful to add that removeCart is called as part of signOut to the comment here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to investigate this more; this change should be resetting customer global state back to initial guest values, so if there are still components rendering like you're logged in, there's a bug somewhere.

Will add that comment and resolve the one area I've observed stale data (left drawer is still pinned like you're logged in). Did you see any other components still rendering with customer data?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the one I noticed too. No others I think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ae8fe42

Copy link
Contributor

@supernova-at supernova-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verification steps

@@ -62,9 +71,10 @@ export const useAuthModal = props => {
}, [showMyAccount]);

const handleSignOut = useCallback(async () => {
setIsSigningOut(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ever go back to false?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do we rely on unmount for that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this callback will trigger a page refresh, performing the reset for us.

@supernova-at
Copy link
Contributor

supernova-at commented May 19, 2020

After adding the second item in the verification steps I'm still appearing as signed in when I open the left drawer.

So I think I'm signed in with item (2) as the only thing in my cart.
But if I log out and log back in, item (1) is the only thing in my cart.

@tjwiebell
Copy link
Contributor

@supernova-at - I didn't even run through those verification steps 🤦 this has been resolved in e41d29f.

@supernova-at
Copy link
Contributor

This is much better - thanks!

I still think there's some awkwardness here, but that may just be the nature of the issue. Off the top of my head I was thinking a toast or something to inform the user that they've been signed out, or better yet a prompt asking if they'd like to "stay signed in".

Since we don't have UX for any of that though, I think there's enough value here to get this in.

@devops-pwa-codebuild
Copy link
Collaborator

Performance Test Results

The following fails have been reported by WebpageTest. These numbers indicates a possible performance issue with the PR which requires further manual testing to validate.

https://pr-2379.pwa-venia.com : LH Performance Expected 0.85 Actual 0.57, LH Best Practices Expected 1 Actual 0.92
https://pr-2379.pwa-venia.com/venia-tops.html : LH Performance Expected 0.75 Actual 0.37, LH Best Practices Expected 1 Actual 0.92
https://pr-2379.pwa-venia.com/valeria-two-layer-tank.html : LH Performance Expected 0.8 Actual 0.51, LH Accessibility Expected 0.9 Actual 0.89, LH Best Practices Expected 1 Actual 0.92

@dpatil-magento dpatil-magento merged commit 2089baa into develop May 20, 2020
@dpatil-magento dpatil-magento deleted the rugh/fix-mini-cart-auth-expiry branch May 20, 2020 22:34
@m2-community-project m2-community-project bot moved this from Review in Progress to Done in Pull Request Progress May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:peregrine pkg:venia-ui version: Patch This changeset includes backwards compatible bug fixes.
Development

Successfully merging this pull request may close these issues.

[bug]: Expired token results in graphql errors in mini cart
6 participants