Fix cart clearing during Stripe checkout redirect
Use sessionStorage flag to prevent LogoutOnUnload from clearing cart and logging out when navigating to Stripe checkout. Changes: - Checkout button sets 'isCheckingOut' flag in sessionStorage before API call - LogoutOnUnload checks this flag and skips logout if checkout in progress - Flag is removed if API returns an error - Flag persists during navigation to Stripe URL This ensures cart and login persist through Stripe payment flow. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 4.5
parent
017b68f17b
commit
eda518d1be
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { useCart } from '@/lib/cartStore';
|
||||
|
||||
/**
|
||||
@@ -10,28 +10,13 @@ import { useCart } from '@/lib/cartStore';
|
||||
*/
|
||||
export default function LogoutOnUnload() {
|
||||
const clearCart = useCart((s) => s.clear);
|
||||
const isCheckingOut = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
// Mark when checkout is being initiated
|
||||
const origFetch = window.fetch;
|
||||
window.fetch = function(...args) {
|
||||
const url = typeof args[0] === 'string' ? args[0] : (args[0] as Request).url;
|
||||
if (url?.includes('/api/checkout')) {
|
||||
isCheckingOut.current = true;
|
||||
}
|
||||
return origFetch.apply(this, args);
|
||||
};
|
||||
|
||||
return () => {
|
||||
window.fetch = origFetch;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const handleUnload = () => {
|
||||
// Don't logout if we're in the middle of checkout (navigating to Stripe)
|
||||
if (isCheckingOut.current) {
|
||||
// The checkout component sets this flag before making the API call
|
||||
const isCheckingOut = typeof window !== 'undefined' && sessionStorage.getItem('isCheckingOut');
|
||||
if (isCheckingOut) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user