Skip to content

Commit

Permalink
fix: clear cart buyer identity on log out
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyagabriel committed Sep 26, 2024
1 parent 25414e6 commit 2a128b6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/lib/customer/hooks/useCustomerLogOut.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {useCallback} from 'react';
import {useFetcher, useNavigate} from '@remix-run/react';
import {useCart} from '@shopify/hydrogen-react';

import {LOGGED_OUT_REDIRECT_TO} from '~/lib/constants';
import {removeCustomerAccessTokenFromLocalStorage} from '~/lib/customer';
import {useGlobal, useLocale} from '~/hooks';

export function useCustomerLogOut() {
const {isPreviewModeEnabled, setPreviewModeCustomer} = useGlobal();
const {buyerIdentityUpdate} = useCart();
const {pathPrefix} = useLocale();
const navigate = useNavigate();
const fetcher = useFetcher({key: 'logout'});

const customerLogOut = useCallback(() => {
buyerIdentityUpdate({customerAccessToken: null});

/* when in customizer, customer is managed through local storage and
* global state, instead of session cookies */
if (isPreviewModeEnabled) {
Expand All @@ -20,7 +24,10 @@ export function useCustomerLogOut() {
return navigate(`${pathPrefix}${LOGGED_OUT_REDIRECT_TO}`);
}

fetcher.submit(null, {method: 'POST', action: '/account/logout'});
fetcher.submit(null, {
method: 'POST',
action: `${pathPrefix}/account/logout`,
});
}, []);

return {customerLogOut};
Expand Down

0 comments on commit 2a128b6

Please sign in to comment.