diff --git a/packages/hydrogen/src/customer/customer.test.ts b/packages/hydrogen/src/customer/customer.test.ts index f3f7693df..93d8a0515 100644 --- a/packages/hydrogen/src/customer/customer.test.ts +++ b/packages/hydrogen/src/customer/customer.test.ts @@ -1392,6 +1392,66 @@ describe('customer', () => { expect(customAuthStatusHandler).toHaveBeenCalledOnce(); } }); + + it('handles Remix `https://localhost/account/orders.data` url extensions when passing current path as param if logged out', async () => { + const customer = createCustomerAccountClient({ + session, + customerAccountId: 'customerAccountId', + customerAccountUrl: 'https://customer-api', + request: new Request('https://localhost/account/orders.data'), + waitUntil: vi.fn(), + }); + (session.get as any).mockReturnValueOnce(undefined); + + try { + await customer.handleAuthStatus(); + } catch (error) { + expect((error as Response).status).toBe(302); + expect((error as Response).headers.get('location')).toBe( + '/account/login?return_to=%2Faccount%2Forders', + ); + } + }); + + it('handles Remix `https://localhost/account/_root.data` url extensions when passing current path as param if logged out', async () => { + const customer = createCustomerAccountClient({ + session, + customerAccountId: 'customerAccountId', + customerAccountUrl: 'https://customer-api', + request: new Request('https://localhost/account/_root.data'), + waitUntil: vi.fn(), + }); + (session.get as any).mockReturnValueOnce(undefined); + + try { + await customer.handleAuthStatus(); + } catch (error) { + expect((error as Response).status).toBe(302); + expect((error as Response).headers.get('location')).toBe( + '/account/login?return_to=%2Faccount', + ); + } + }); + + it('handles Remix `https://localhost/_root.data` url extensions when passing current path as param if logged out', async () => { + const customer = createCustomerAccountClient({ + session, + customerAccountId: 'customerAccountId', + customerAccountUrl: 'https://customer-api', + request: new Request('https://localhost/_root.data'), + waitUntil: vi.fn(), + }); + (session.get as any).mockReturnValueOnce(undefined); + + try { + await customer.handleAuthStatus(); + } catch (error) { + expect((error as Response).status).toBe(302); + expect((error as Response).headers.get('location')).toBe( + '/account/login?return_to=%2F', + ); + } + }); }); describe('query', () => { diff --git a/packages/hydrogen/src/customer/customer.ts b/packages/hydrogen/src/customer/customer.ts index 622e86539..20299f7ba 100644 --- a/packages/hydrogen/src/customer/customer.ts +++ b/packages/hydrogen/src/customer/customer.ts @@ -71,7 +71,8 @@ function defaultAuthStatusHandler( */ const cleanedPathname = pathname .replace(/\.data$/, '') - .replace(/^\/_root$/, '/'); + .replace(/\/_root$/, '/') + .replace(/(.+)\/$/, '$1'); const redirectTo = defaultLoginUrl +