Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Move password resets to top-level Store API route
Damian Legawiecb52e666fb30b
diff --git a/packages/next/src/actions/auth.ts b/packages/next/src/actions/auth.tsindex 0ac2163d8f..969f65589b 100644--- a/packages/next/src/actions/auth.ts+++ b/packages/next/src/actions/auth.ts@@ -145,7 +145,7 @@ export async function requestPasswordReset(email: string,redirectUrl?: string): Promise<{ message: string }> {- return getClient().customer.passwordResets.create({+ return getClient().passwordResets.create({email,...(redirectUrl && { redirect_url: redirectUrl }),});@@ -161,7 +161,7 @@ export async function resetPassword(password_confirmation: string): Promise<{ success: boolean; error?: string }> {try {- const result = await getClient().customer.passwordResets.update(token, {+ const result = await getClient().passwordResets.update(token, {password,password_confirmation,});diff --git a/packages/sdk/src/store-client.ts b/packages/sdk/src/store-client.tsindex 2e0f6e1fc9..7b9a4f3d35 100644--- a/packages/sdk/src/store-client.ts+++ b/packages/sdk/src/store-client.ts@@ -28,6 +28,7 @@ import type {RequestPasswordResetParams,ResetPasswordParams,Cart,+ CartResponse,CreditCard,GiftCard,Product,@@ -302,15 +303,15 @@ export class StoreClient {* Get a cart by prefixed ID* @param cartId - Cart prefixed ID (e.g., "cart_abc123")*/- get: (cartId: string, options?: RequestOptions): Promise<Cart> =>- this.request<Cart>('GET', `/carts/${cartId}`, options),+ get: (cartId: string, options?: RequestOptions): Promise<CartResponse> =>+ this.request<CartResponse>('GET', `/carts/${cartId}`, options),/*** Create a new cart* @param params - Optional cart parameters (e.g., metadata, items)*/- create: (params?: CreateCartParams, options?: RequestOptions): Promise<Cart> =>- this.request<Cart>('POST', '/carts', {+ create: (params?: CreateCartParams, options?: RequestOptions): Promise<CartResponse> =>+ this.request<CartResponse>('POST', '/carts', {...options,body: params,}),@@ -324,8 +325,8 @@ export class StoreClient {cartId: string,params: UpdateCartParams,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>('PATCH', `/carts/${cartId}`, {+ ): Promise<CartResponse> =>+ this.request<CartResponse>('PATCH', `/carts/${cartId}`, {...options,body: params,}),@@ -342,8 +343,8 @@ export class StoreClient {* @param cartId - Cart prefixed ID* @param options - Must include `token` (JWT) for authentication*/- associate: (cartId: string, options: RequestOptions): Promise<Cart> =>- this.request<Cart>('PATCH', `/carts/${cartId}/associate`, options),+ associate: (cartId: string, options: RequestOptions): Promise<CartResponse> =>+ this.request<CartResponse>('PATCH', `/carts/${cartId}/associate`, options),/*** Complete the cart and finalize the purchase.@@ -366,8 +367,8 @@ export class StoreClient {cartId: string,params: AddLineItemParams,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>('POST', `/carts/${cartId}/items`, {+ ): Promise<CartResponse> =>+ this.request<CartResponse>('POST', `/carts/${cartId}/items`, {...options,body: params,}),@@ -382,8 +383,8 @@ export class StoreClient {lineItemId: string,params: UpdateLineItemParams,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>(+ ): Promise<CartResponse> =>+ this.request<CartResponse>('PATCH',`/carts/${cartId}/items/${lineItemId}`,{ ...options, body: params }@@ -398,8 +399,8 @@ export class StoreClient {cartId: string,lineItemId: string,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>(+ ): Promise<CartResponse> =>+ this.request<CartResponse>('DELETE',`/carts/${cartId}/items/${lineItemId}`,options@@ -419,8 +420,8 @@ export class StoreClient {cartId: string,code: string,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>('POST', `/carts/${cartId}/discount_codes`, {+ ): Promise<CartResponse> =>+ this.request<CartResponse>('POST', `/carts/${cartId}/discount_codes`, {...options,body: { code },}),@@ -434,8 +435,8 @@ export class StoreClient {cartId: string,code: string,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>(+ ): Promise<CartResponse> =>+ this.request<CartResponse>('DELETE',`/carts/${cartId}/discount_codes/${code}`,options@@ -457,8 +458,8 @@ export class StoreClient {cartId: string,code: string,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>('POST', `/carts/${cartId}/gift_cards`, {+ ): Promise<CartResponse> =>+ this.request<CartResponse>('POST', `/carts/${cartId}/gift_cards`, {...options,body: { code },}),@@ -472,8 +473,8 @@ export class StoreClient {cartId: string,giftCardId: string,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>(+ ): Promise<CartResponse> =>+ this.request<CartResponse>('DELETE',`/carts/${cartId}/gift_cards/${giftCardId}`,options@@ -494,8 +495,8 @@ export class StoreClient {fulfillmentId: string,params: { selected_delivery_rate_id: string },options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>(+ ): Promise<CartResponse> =>+ this.request<CartResponse>('PATCH',`/carts/${cartId}/fulfillments/${fulfillmentId}`,{ ...options, body: params }@@ -605,8 +606,8 @@ export class StoreClient {cartId: string,amount?: number,options?: RequestOptions- ): Promise<Cart> =>- this.request<Cart>('POST', `/carts/${cartId}/store_credits`, {+ ): Promise<CartResponse> =>+ this.request<CartResponse>('POST', `/carts/${cartId}/store_credits`, {...options,body: amount ? { amount } : undefined,}),@@ -615,8 +616,8 @@ export class StoreClient {* Remove store credit from the cart* @param cartId - Cart prefixed ID*/- remove: (cartId: string, options?: RequestOptions): Promise<Cart> =>- this.request<Cart>(+ remove: (cartId: string, options?: RequestOptions): Promise<CartResponse> =>+ this.request<CartResponse>('DELETE',`/carts/${cartId}/store_credits`,options@@ -893,34 +894,36 @@ export class StoreClient {),},+ };++ // ============================================+ // Password Resets+ // ============================================++ readonly passwordResets = {/**- * Password reset+ * Request a password reset email.+ * Always succeeds (202) to prevent email enumeration.*/- passwordResets: {- /**- * Request a password reset email.- * Always succeeds (202) to prevent email enumeration.- */- create: (params: RequestPasswordResetParams): Promise<{ message: string }> =>- this.request<{ message: string }>('POST', '/customers/me/password_resets', {- body: params,- }),+ create: (params: RequestPasswordResetParams): Promise<{ message: string }> =>+ this.request<{ message: string }>('POST', '/password_resets', {+ body: params,+ }),- /**- * Reset password using the token from the email.- * Returns a JWT token on success (auto-login).- * @param token - Password reset token from the email- */- update: (- token: string,- params: ResetPasswordParams- ): Promise<AuthTokens> =>- this.request<AuthTokens>(- 'PATCH',- `/customers/me/password_resets/${token}`,- { body: params }- ),- },+ /**+ * Reset password using the token from the email.+ * Returns a JWT token on success (auto-login).+ * @param token - Password reset token from the email+ */+ update: (+ token: string,+ params: ResetPasswordParams+ ): Promise<AuthTokens> =>+ this.request<AuthTokens>(+ 'PATCH',+ `/password_resets/${token}`,+ { body: params }+ ),};// ============================================diff --git a/packages/sdk/tests/mocks/handlers.ts b/packages/sdk/tests/mocks/handlers.tsindex 9b2a308c37..eed93719de 100644--- a/packages/sdk/tests/mocks/handlers.ts+++ b/packages/sdk/tests/mocks/handlers.ts@@ -514,15 +514,15 @@ export const handlers = [})),- // Customer > Password Resets- http.post(`${API_PREFIX}/customers/me/password_resets`, () =>+ // Password Resets+ http.post(`${API_PREFIX}/password_resets`, () =>HttpResponse.json({ message: 'If an account exists for that email, password reset instructions have been sent.' },{ status: 202 })),- http.patch(`${API_PREFIX}/customers/me/password_resets/:token`, () =>+ http.patch(`${API_PREFIX}/password_resets/:token`, () =>HttpResponse.json({ token: 'new-jwt-token', user: fixtures.user })),diff --git a/packages/sdk/tests/password-resets.test.ts b/packages/sdk/tests/password-resets.test.tsindex 3977a0d721..9ff254a96f 100644--- a/packages/sdk/tests/password-resets.test.ts+++ b/packages/sdk/tests/password-resets.test.ts@@ -5,11 +5,11 @@ import { createTestClient, TEST_BASE_URL } from './helpers';const API_PREFIX = `${TEST_BASE_URL}/api/v3/store`;-describe('customer.passwordResets', () => {+describe('passwordResets', () => {describe('create (request password reset)', () => {it('returns a message on success', async () => {const client = createTestClient();- const result = await client.customer.passwordResets.create({+ const result = await client.passwordResets.create({email: '[email protected]',});@@ -19,7 +19,7 @@ describe('customer.passwordResets', () => {it('sends email in request body', async () => {let capturedBody: Record<string, unknown> = {};server.use(- http.post(`${API_PREFIX}/customers/me/password_resets`, async ({ request }) => {+ http.post(`${API_PREFIX}/password_resets`, async ({ request }) => {capturedBody = await request.json() as Record<string, unknown>;return HttpResponse.json({ message: 'If an account exists for that email, password reset instructions have been sent.' },@@ -29,7 +29,7 @@ describe('customer.passwordResets', () => {);const client = createTestClient();- await client.customer.passwordResets.create({ email: '[email protected]' });+ await client.passwordResets.create({ email: '[email protected]' });expect(capturedBody.email).toBe('[email protected]');});@@ -38,7 +38,7 @@ describe('customer.passwordResets', () => {describe('update (reset password with token)', () => {it('returns auth tokens on successful reset', async () => {const client = createTestClient();- const result = await client.customer.passwordResets.update(+ const result = await client.passwordResets.update('valid-reset-token',{password: 'newsecurepassword',@@ -53,24 +53,24 @@ describe('customer.passwordResets', () => {it('sends token in URL path', async () => {let capturedUrl = '';server.use(- http.patch(`${API_PREFIX}/customers/me/password_resets/:token`, ({ request }) => {+ http.patch(`${API_PREFIX}/password_resets/:token`, ({ request }) => {capturedUrl = request.url;return HttpResponse.json({ token: 'new-jwt-token', user: { id: 'user_1', email: '[email protected]', first_name: null, last_name: null } });}));const client = createTestClient();- await client.customer.passwordResets.update('my-reset-token', {+ await client.passwordResets.update('my-reset-token', {password: 'newsecurepassword',password_confirmation: 'newsecurepassword',});- expect(capturedUrl).toContain('/customers/me/password_resets/my-reset-token');+ expect(capturedUrl).toContain('/password_resets/my-reset-token');});it('throws SpreeError on invalid token', async () => {server.use(- http.patch(`${API_PREFIX}/customers/me/password_resets/:token`, () =>+ http.patch(`${API_PREFIX}/password_resets/:token`, () =>HttpResponse.json({ error: { code: 'password_reset_token_invalid', message: 'Password reset token is invalid or has expired.' } },{ status: 422 }@@ -80,7 +80,7 @@ describe('customer.passwordResets', () => {const client = createTestClient();try {- await client.customer.passwordResets.update('expired-token', {+ await client.passwordResets.update('expired-token', {password: 'newsecurepassword',password_confirmation: 'newsecurepassword',});diff --git a/spree/api/app/controllers/spree/api/v3/store/customer/password_resets_controller.rb b/spree/api/app/controllers/spree/api/v3/store/customer/password_resets_controller.rbindex b2f54eed4f..cf1393ce3a 100644--- a/spree/api/app/controllers/spree/api/v3/store/customer/password_resets_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/store/customer/password_resets_controller.rb@@ -13,7 +13,7 @@ module Spreeskip_before_action :authenticate_user- # POST /api/v3/store/customer/password_resets+ # POST /api/v3/store/password_resetsdef createredirect_url = params[:redirect_url]@@ -41,7 +41,7 @@ module Spreerender json: { message: Spree.t(:password_reset_requested, scope: :api) }, status: :acceptedend- # PATCH /api/v3/store/customer/password_resets/:id+ # PATCH /api/v3/store/password_resets/:iddef updateuser = Spree.user_class.find_by_password_reset_token(params[:id])diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rbindex 0d4dec5f38..4b4b716fbb 100644--- a/spree/api/config/routes.rb+++ b/spree/api/config/routes.rb@@ -53,6 +53,9 @@ Spree::Core::Engine.add_routes do# Orders (single order lookup, guest-accessible via order token)resources :orders, only: [:show]+ # Password Resets (top-level, no auth required)+ resources :password_resets, only: [:create, :update], controller: 'customer/password_resets'+# Customersresources :customers, only: [:create]@@ -61,8 +64,6 @@ Spree::Core::Engine.add_routes doget '/', action: :show, controller: '/spree/api/v3/store/customers'patch '/', action: :update, controller: '/spree/api/v3/store/customers'- resources :password_resets, only: [:create, :update]-resources :orders, only: [:index, :show]resources :addresses, only: [:index, :show, :create, :update, :destroy]resources :credit_cards, only: [:index, :show, :destroy]diff --git a/spree/api/spec/integration/spree/api/v3/store/customer/password_resets_spec.rb b/spree/api/spec/integration/spree/api/v3/store/customer/password_resets_spec.rbindex b6c286a02e..6d45fe8bd9 100644--- a/spree/api/spec/integration/spree/api/v3/store/customer/password_resets_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/store/customer/password_resets_spec.rb@@ -7,7 +7,7 @@ RSpec.describe 'Password Resets API', type: :request, swagger_doc: 'api-referenclet(:existing_user) { create(:user, email: '[email protected]', password: 'password123') }- path '/api/v3/store/customers/me/password_resets' do+ path '/api/v3/store/password_resets' dopost 'Request a password reset' dotags 'Authentication'consumes 'application/json'@@ -16,7 +16,7 @@ RSpec.describe 'Password Resets API', type: :request, swagger_doc: 'api-referencdescription 'Sends a password reset email if an account exists for the given email address. Always returns 202 Accepted to prevent email enumeration.'sdk_example <<~JS- await client.customer.passwordResets.create({+ await client.passwordResets.create({email: '[email protected]',redirect_url: 'https://myshop.com/reset-password',})@@ -73,7 +73,7 @@ RSpec.describe 'Password Resets API', type: :request, swagger_doc: 'api-referencendend- path '/api/v3/store/customers/me/password_resets/{token}' do+ path '/api/v3/store/password_resets/{token}' dopatch 'Reset password with token' dotags 'Authentication'consumes 'application/json'@@ -82,7 +82,7 @@ RSpec.describe 'Password Resets API', type: :request, swagger_doc: 'api-referencdescription 'Resets the password using a token received via email. Returns a JWT token on success (auto-login).'sdk_example <<~JS- const auth = await client.customer.passwordResets.update(+ const auth = await client.passwordResets.update('reset-token-from-email',{password: 'newsecurepassword',