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
Require a password on Store API customer self-registration
Łukasz Adamczyk722db0bf42a5
diff --git a/spree/api/app/controllers/spree/api/v3/store/customers_controller.rb b/spree/api/app/controllers/spree/api/v3/store/customers_controller.rbindex cf6b1153ed..3cb6b572af 100644--- a/spree/api/app/controllers/spree/api/v3/store/customers_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/store/customers_controller.rb@@ -13,6 +13,16 @@ module Spreedef createuser = Spree.customer_class.new(permitted_params.except(:current_password))+ # The model allows a blank password (admin-created customers claim+ # their account via password reset), so self-registration — which+ # issues a JWT + refresh token on success — must require one itself,+ # otherwise a caller with the publishable key could obtain an+ # authenticated session for any unclaimed email.+ if user.password.blank?+ user.errors.add(:password, :blank)+ return render_errors(user.errors)+ end+if user.savelink_matching_newsletter_subscriber!(user)refresh_token = Spree::RefreshToken.create_for(user, request_env: {diff --git a/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rbindex a957ac14b5..36adfc144d 100644--- a/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/customers_controller_spec.rb@@ -128,6 +128,28 @@ RSpec.describe Spree::Api::V3::Store::CustomersController, type: :controller doexpect(json_response['error']['code']).to eq('validation_error')expect(json_response['error']['details']['email']).to be_presentend++ it 'rejects a blank password without creating a customer or issuing tokens' do+ expect {+ post :create, params: { email: '[email protected]', password: '', password_confirmation: '' }+ }.not_to change(Spree.customer_class, :count)++ expect(response).to have_http_status(:unprocessable_content)+ expect(json_response['error']['code']).to eq('validation_error')+ expect(json_response['error']['details']['password']).to be_present+ expect(json_response).not_to include('token', 'refresh_token')+ end++ it 'rejects an omitted password without creating a customer or issuing tokens' do+ expect {+ post :create, params: { email: '[email protected]' }+ }.not_to change(Spree.customer_class, :count)++ expect(response).to have_http_status(:unprocessable_content)+ expect(json_response['error']['code']).to eq('validation_error')+ expect(json_response['error']['details']['password']).to be_present+ expect(json_response).not_to include('token', 'refresh_token')+ endendcontext 'without API key' do