Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Remove Devise and the legacy auth models (platform-auth Phase 5)

Łukasz Adamczyk104e5e44e30b
diff --git a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb
index 679ce5193b..0a568a26aa 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb
@@ -11,13 +11,9 @@ module Spree
def create
@resource = Spree.customer_class.new(permitted_params)
- # Admin-created customers don't pick a password upfront — they
- # claim the account via password reset later.
- # `Spree::CustomerMethods` exposes `skip_password_validation` so
- # Devise's `:validatable` lets a nil credential through on this
- # code path. Storefront registration never sets the flag, so
- # customer self-signup still requires a password.
- @resource.skip_password_validation = true if @resource.password.blank?
+ # Admin-created customers may be created without a password; they
+ # claim the account via password reset later (the model does not
+ # enforce password presence).
authorize!(:create, @resource)
if @resource.save
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb
index e8876d6992..7b1e471f15 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb
@@ -88,10 +88,9 @@ RSpec.describe Spree::Api::V3::Admin::CustomersController, type: :controller do
# Admin-created customers don't pick a password upfront — the merchant
# adds the profile and the customer claims the account via a separate
- # password-reset flow. The host app's `Spree::User` is Devise-
- # validatable; `Spree::UserMethods` exposes `skip_password_validation`
- # so the admin controller can opt out of the presence check on create.
- # The Store API registration path stays untouched (see store
+ # password-reset flow. The customer model uses `has_secure_password
+ # validations: false`, so password presence is not enforced on create.
+ # The Store API registration path enforces it separately (see store
# customers_controller spec).
context 'without password' do
let(:no_password_params) { { email: '[email protected]', first_name: 'Pat' } }
@@ -155,11 +154,10 @@ RSpec.describe Spree::Api::V3::Admin::CustomersController, type: :controller do
end
end
- # PATCH without a password must not blank an existing credential. Devise's
- # `password_required?` already skips presence on persisted records when
- # password is nil, so this is the regression guard for that contract —
- # ensures a profile-only edit doesn't accidentally invalidate the
- # customer's ability to log in.
+ # PATCH without a password must not blank an existing credential.
+ # has_secure_password only rewrites the digest when a non-blank password is
+ # supplied, so this is the regression guard — a profile-only edit must not
+ # accidentally invalidate the customer's ability to log in.
context 'without password' do
it 'updates profile fields without touching the existing credential' do
original_digest = customer.password_digest
diff --git a/spree/core/app/models/concerns/spree/admin_user_methods.rb b/spree/core/app/models/concerns/spree/admin_user_methods.rb
index b26e4cb244..d64adea7b1 100644
--- a/spree/core/app/models/concerns/spree/admin_user_methods.rb
+++ b/spree/core/app/models/concerns/spree/admin_user_methods.rb
@@ -7,11 +7,6 @@ module Spree
include Spree::RansackableAttributes
included do
- # Wins over Devise's implementation regardless of module include order, so
- # host apps that add `devise ...` after this concern still send admin auth
- # emails through Spree's mailer.
- prepend Spree::AdminUserMethods::DeviseNotifications
-
has_prefix_id :adm
has_person_name
@@ -24,7 +19,7 @@ module Spree
# `try` rather than `&.`: a model that lacks the method/column entirely
# must fall through rather than raise NameError. `password_salt` covers
# has_secure_password (which derives it from password_digest);
- # `encrypted_password` covers legacy bcrypt / Devise models.
+ # `encrypted_password` covers a legacy bcrypt column on a custom model.
try(:password_salt)&.last(10) || try(:encrypted_password)&.last(10)
end
@@ -103,25 +98,5 @@ module Spree
reports.destroy_all
exports.destroy_all
end
-
- # Routes Devise's auth notifications through Spree's own mailer so admin
- # password reset and account confirmation emails carry the store design and
- # locale instead of Devise's built-in templates. Other notification kinds
- # (e.g. unlock instructions) fall through to Devise.
- module DeviseNotifications
- def send_devise_notification(notification, *args)
- token = args.first
- store = Spree::Current.store || Spree::Store.default
-
- case notification
- when :reset_password_instructions
- Spree::AdminUserMailer.password_reset_email(self, token, store).deliver_later
- when :confirmation_instructions
- Spree::AdminUserMailer.confirmation_email(self, token, store).deliver_later
- else
- super
- end
- end
- end
end
end
diff --git a/spree/core/app/models/concerns/spree/customer_methods.rb b/spree/core/app/models/concerns/spree/customer_methods.rb
index ba37fa18b5..ce9fc9836c 100644
--- a/spree/core/app/models/concerns/spree/customer_methods.rb
+++ b/spree/core/app/models/concerns/spree/customer_methods.rb
@@ -13,35 +13,9 @@ module Spree
include Spree::Searchable
include Spree::Publishable
- # Opt-in escape hatch for admin-side customer creation. When the host
- # `Spree::User` includes Devise's `:validatable`, password presence is
- # enforced on every create; admin-created customers don't pick a
- # password upfront — they claim the account later via password reset.
- # The admin Customers controller flips `skip_password_validation = true`
- # when no password was supplied; the storefront registration path never
- # sets it, so customer self-signup still requires a password.
- #
- # Prepended (not just defined) so it sits above Devise's own
- # `password_required?` and can fall through with `super` when the flag
- # isn't set. On `LegacyUser` (gem default, no Devise) `super` raises
- # NoMethodError if invoked — but the early return covers the only
- # branch that ever reaches this method on that path, and `LegacyUser`
- # doesn't call `password_required?` at all, so the override is a no-op
- # there.
- module SkipPasswordValidation
- def password_required?
- return false if skip_password_validation && try(:encrypted_password).blank? && password.blank?
- super
- end
- end
-
included do
has_prefix_id :cust # Stripe: cus_
- attr_accessor :skip_password_validation
-
- prepend SkipPasswordValidation
-
# Enable lifecycle events for user models
publishes_lifecycle_events
@@ -52,7 +26,7 @@ module Spree
# `try` rather than `&.`: a model that lacks the method/column entirely
# must fall through rather than raise NameError. `password_salt` covers
# has_secure_password (which derives it from password_digest);
- # `encrypted_password` covers legacy bcrypt / Devise models.
+ # `encrypted_password` covers a legacy bcrypt column on a custom model.
try(:password_salt)&.last(10) || try(:encrypted_password)&.last(10)
end
diff --git a/spree/core/app/models/spree/legacy_admin_user.rb b/spree/core/app/models/spree/legacy_admin_user.rb
deleted file mode 100644
index 09a700a50b..0000000000
--- a/spree/core/app/models/spree/legacy_admin_user.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-# Default implementation of AdminUser for staff/admin accounts.
-# This class is separate from LegacyUser to support distinct customer vs staff models.
-require 'bcrypt'
-
-module Spree
- class LegacyAdminUser < Spree.base_class
- include Spree::AdminUserMethods
-
- self.table_name = 'spree_admin_users'
-
- attr_accessor :password, :password_confirmation
-
- validates :email, presence: true, uniqueness: { case_sensitive: false }
-
- before_save :encrypt_password, if: :password
-
- # Simple password validation for testing purposes
- # In production, Spree.admin_user_class should be overridden with a proper auth solution (e.g., Devise)
- def valid_password?(check_password)
- return false if encrypted_password.blank?
-
- BCrypt::Password.new(encrypted_password) == check_password
- end
-
- private
-
- def encrypt_password
- self.encrypted_password = BCrypt::Password.create(password)
- end
- end
-end
diff --git a/spree/core/app/models/spree/legacy_user.rb b/spree/core/app/models/spree/legacy_user.rb
deleted file mode 100644
index 60eaa17cb7..0000000000
--- a/spree/core/app/models/spree/legacy_user.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-# Default implementation of User.
-require 'bcrypt'
-
-module Spree
- class LegacyUser < Spree.base_class
- include Spree::UserAddress
- include Spree::UserPaymentSource
- include Spree::CustomerMethods
-
- self.table_name = 'spree_users'
-
- attr_accessor :password, :password_confirmation
-
- validates :email, presence: true, uniqueness: { case_sensitive: false }
- validates :password, confirmation: true, if: :password
-
- before_save :encrypt_password, if: :password
-
- # Simple password validation for testing purposes
- # In production, Spree.customer_class should be overridden with a proper auth solution (e.g., Devise)
- def valid_password?(check_password)
- return false if encrypted_password.blank?
-
- BCrypt::Password.new(encrypted_password) == check_password
- end
-
- private
-
- def encrypt_password
- self.encrypted_password = BCrypt::Password.create(password)
- end
- end
-end
diff --git a/spree/core/lib/generators/spree/authentication/custom/custom_generator.rb b/spree/core/lib/generators/spree/authentication/custom/custom_generator.rb
deleted file mode 100644
index e42eb27684..0000000000
--- a/spree/core/lib/generators/spree/authentication/custom/custom_generator.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-require 'rails/generators'
-
-module Spree
- module Authentication
- class CustomGenerator < Rails::Generators::Base
- desc 'Set up a Spree installation with custom authentication'
-
- def self.source_paths
- paths = superclass.source_paths
- paths << File.expand_path('templates', __dir__)
- paths.flatten
- end
-
- def generate
- template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'
-
- file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
- send(file_action, 'config/initializers/spree.rb') do
- %Q{
- Rails.application.config.to_prepare do
- require_dependency 'spree/authentication_helpers'
- end\n}
- end
-
- user_class_file = Rails.root.join('app', 'models', "#{Spree.customer_class.name.underscore}.rb")
-
- if File.exist?(user_class_file)
- inject_into_file user_class_file, after: "class #{Spree.customer_class.name} < ApplicationRecord\n" do
- <<-RUBY
- # Spree modules
- include Spree::UserAddress
- include Spree::CustomerMethods
- include Spree::UserPaymentSource
- RUBY
- end
- say "Successfully added Spree user modules into #{user_class_file}"
- else
- say "Could not locate user model file at #{user_class_file}. Please add these lines manually:", :red
- say <<~RUBY
- # Spree modules
- include Spree::UserAddress
- include Spree::CustomerMethods
- include Spree::UserPaymentSource
- RUBY
- end
- end
- end
- end
-end
diff --git a/spree/core/lib/generators/spree/authentication/custom/templates/authentication_helpers.rb.tt b/spree/core/lib/generators/spree/authentication/custom/templates/authentication_helpers.rb.tt
deleted file mode 100644
index d3a4ad5b1f..0000000000
--- a/spree/core/lib/generators/spree/authentication/custom/templates/authentication_helpers.rb.tt
+++ /dev/null
@@ -1,50 +0,0 @@
-module Spree
- module AuthenticationHelpers
- def self.included(receiver)
- receiver.helper_method(
- :spree_current_user,
- :spree_login_path,
- :spree_signup_path,
- :spree_logout_path,
- :spree_forgot_password_path,
- :spree_edit_password_path,
- :spree_admin_login_path,
- :spree_admin_logout_path
- )
- end
-
- def spree_current_user
- current_user
- end
-
- def spree_login_path(opts = {})
- main_app.login_path(opts)
- end
-
- def spree_signup_path(opts = {})
- main_app.signup_path(opts = {})
- end
-
- def spree_logout_path(opts = {})
- main_app.logout_path(opts = {})
- end
-
- def spree_forgot_password_path(opts = {})
- main_app.forgot_password_path(opts)
- end
-
- def spree_edit_password_path(opts = {})
- main_app.spree_edit_password_path(opts)
- end
-
- def spree_admin_login_path(opts = {})
- spree_login_path(opts)
- end
-
- def spree_admin_logout_path(opts = {})
- spree_logout_path(opts)
- end
- end
-end
-
-ApplicationController.include Spree::AuthenticationHelpers if defined?(ApplicationController)
\ No newline at end of file
diff --git a/spree/core/lib/generators/spree/authentication/devise/devise_generator.rb b/spree/core/lib/generators/spree/authentication/devise/devise_generator.rb
deleted file mode 100644
index e3bef17fa6..0000000000
--- a/spree/core/lib/generators/spree/authentication/devise/devise_generator.rb
+++ /dev/null
@@ -1,84 +0,0 @@
-require 'rails/generators'
-
-module Spree
- module Authentication
- class DeviseGenerator < Rails::Generators::Base
- desc 'Set up a Spree installation with Devise as authentication'
-
- def self.source_paths
- paths = superclass.source_paths
- paths << File.expand_path('templates', __dir__)
- paths.flatten
- end
-
- def generate
- template 'authentication_helpers.rb.tt', 'lib/spree/authentication_helpers.rb'
-
- file_action = File.exist?('config/initializers/spree.rb') ? :append_file : :create_file
- send(file_action, 'config/initializers/spree.rb') do
- %Q{
- Rails.application.config.to_prepare do
- require_dependency 'spree/authentication_helpers'
- end\n}
- end
-
- user_class_file = Rails.root.join('app', 'models', "#{Spree.customer_class.name.underscore}.rb")
-
- if File.exist?(user_class_file)
- inject_into_file user_class_file, after: "class #{Spree.customer_class.name} < ApplicationRecord\n" do
- <<-RUBY
- # Spree modules
- include Spree::UserAddress
- include Spree::CustomerMethods
- include Spree::UserPaymentSource
- RUBY
- end
- gsub_file user_class_file, "< ApplicationRecord", "< Spree.base_class"
-
- say "Successfully added Spree user modules into #{user_class_file}"
- else
- say "Could not locate user model file at #{user_class_file}. Please add these lines manually:", :red
- say <<~RUBY
- # Spree modules
- include Spree::UserAddress
- include Spree::CustomerMethods
- include Spree::UserPaymentSource
- RUBY
-
- say "Please replace < ApplicationRecord with < Spree.base_class in #{user_class_file}"
- end
-
- if Spree.admin_user_class != Spree.customer_class
- admin_user_class_file = Rails.root.join('app', 'models', "#{Spree.admin_user_class.name.underscore}.rb")
-
- if File.exist?(admin_user_class_file)
- inject_into_file admin_user_class_file, after: "class #{Spree.admin_user_class.name} < ApplicationRecord\n" do
- <<-RUBY
- # Spree modules
- include Spree::AdminUserMethods
- RUBY
- end
- gsub_file admin_user_class_file, "< ApplicationRecord", "< Spree.base_class"
-
- say "Successfully added Spree admin user modules into #{admin_user_class_file}"
- else
- say "Could not locate admin user model file at #{admin_user_class_file}. Please add these lines manually:", :red
- say <<~RUBY
- # Spree modules
- include Spree::AdminUserMethods
- RUBY
-
- say "Please replace < ApplicationRecord with < Spree.base_class in #{admin_user_class_file}"
- end
- end
-
- append_file 'config/initializers/spree.rb' do
- %Q{
- if defined?(Devise) && Devise.respond_to?(:parent_controller)
- Devise.parent_controller = "Spree::BaseController"
- end\n}
- end
- end
- end
- end
-end
diff --git a/spree/core/lib/generators/spree/authentication/devise/templates/authentication_helpers.rb.tt b/spree/core/lib/generators/spree/authentication/devise/templates/authentication_helpers.rb.tt
deleted file mode 100644
index 75a3d06e96..0000000000
--- a/spree/core/lib/generators/spree/authentication/devise/templates/authentication_helpers.rb.tt
+++ /dev/null
@@ -1,50 +0,0 @@
-module Spree
- module AuthenticationHelpers
- def self.included(receiver)
- receiver.helper_method(
- :spree_current_user,
- :spree_login_path,
- :spree_signup_path,
- :spree_logout_path,
- :spree_forgot_password_path,
- :spree_edit_password_path,
- :spree_admin_login_path,
- :spree_admin_logout_path
- )
- end
-
- def spree_current_user
- send("current_#{Spree.customer_class.model_name.singular_route_key}")
- end
-
- def spree_login_path(opts = {})
- new_session_path(Spree.customer_class.model_name.singular_route_key, opts)
- end
-
- def spree_signup_path(opts = {})
- new_registration_path(Spree.customer_class.model_name.singular_route_key, opts)
- end
-
- def spree_logout_path(opts = {})
- destroy_session_path(Spree.customer_class.model_name.singular_route_key, opts)
- end
-
- def spree_forgot_password_path(opts = {})
- new_password_path(Spree.customer_class.model_name.singular_route_key, opts)
- end
-
- def spree_edit_password_path(opts = {})
- edit_registration_path(Spree.customer_class.model_name.singular_route_key, opts)
- end
-
- def spree_admin_login_path(opts = {})
- new_session_path(Spree.admin_user_class.model_name.singular_route_key, opts)
- end
-
- def spree_admin_logout_path(opts = {})
- destroy_session_path(Spree.admin_user_class.model_name.singular_route_key, opts)
- end
- end
-end
-
-ApplicationController.include Spree::AuthenticationHelpers if defined?(ApplicationController)
diff --git a/spree/core/lib/spree/testing_support/common_rake.rb b/spree/core/lib/spree/testing_support/common_rake.rb
index fa45783038..d6e37b1d95 100644
--- a/spree/core/lib/spree/testing_support/common_rake.rb
+++ b/spree/core/lib/spree/testing_support/common_rake.rb
@@ -56,14 +56,6 @@ namespace :common do
system('yes | bundle exec rails importmap:install turbo:install stimulus:install')
end
- # install devise if it's not the legacy user, useful for testing storefront
- if args[:authentication] == 'devise' && args[:user_class] != 'Spree::LegacyUser'
- system('bundle exec rails g devise:install --force --auto-accept')
- system("bundle exec rails g devise #{args[:user_class]} --force --auto-accept")
- system("bundle exec rails g devise #{args[:admin_user_class]} --force --auto-accept") if args[:admin_user_class].present? && args[:admin_user_class] != args[:user_class]
- system('rm -rf spec') # we need to cleanup factories created by devise to avoid naming conflict
- end
-
# Run core Spree install generator
# The spree:install generator lives in the root spree gem. Core gems (spree_core, spree_api)
# don't have spree as a dependency, so we need to use the root Gemfile to access the generator.
diff --git a/spree/core/spec/models/spree/base_spec.rb b/spree/core/spec/models/spree/base_spec.rb
index f275df00a3..75fba3a86f 100644
--- a/spree/core/spec/models/spree/base_spec.rb
+++ b/spree/core/spec/models/spree/base_spec.rb
@@ -95,8 +95,8 @@ describe Spree::Base do
describe '.json_api_columns' do
it 'skips sensitive data' do
- expect(Spree::LegacyUser.json_api_columns).not_to include('password')
- expect(Spree::LegacyUser.json_api_columns).to include('email')
+ expect(Spree.customer_class.json_api_columns).not_to include('password')
+ expect(Spree.customer_class.json_api_columns).to include('email')
end
it { expect(Spree::Address.json_api_columns).to contain_exactly('address1', 'address2', 'alternative_phone', 'city', 'company', 'created_at', 'deleted_at', 'firstname', 'label', 'lastname', 'phone', 'state_name', 'updated_at', 'zipcode', 'public_metadata', 'private_metadata', 'quick_checkout', 'latitude', 'longitude') }
diff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rb
index 64e7984b32..af4e6cd6c7 100644
--- a/spree/core/spec/models/spree/order_spec.rb
+++ b/spree/core/spec/models/spree/order_spec.rb
@@ -13,7 +13,7 @@ describe Spree::Order, type: :model do
let!(:store) { @default_store }
let(:order) { create(:order, customer: user, store: store) }
- before { allow(Spree::LegacyUser).to receive_messages(current: build(:user)) }
+ before { allow(Spree.customer_class).to receive_messages(current: build(:user)) }
it_behaves_like 'metadata'
diff --git a/spree/core/spec/models/spree/promotion/rules/one_use_per_user_spec.rb b/spree/core/spec/models/spree/promotion/rules/one_use_per_user_spec.rb
index 802ee13881..2f9a3d0571 100644
--- a/spree/core/spec/models/spree/promotion/rules/one_use_per_user_spec.rb
+++ b/spree/core/spec/models/spree/promotion/rules/one_use_per_user_spec.rb
@@ -7,7 +7,7 @@ describe Spree::Promotion::Rules::OneUsePerUser, type: :model do
subject { rule.eligible?(order) }
let(:order) { double Spree::Order, user: user }
- let(:user) { double Spree::LegacyUser }
+ let(:user) { double Spree.customer_class }
let(:promotion) { create(:promotion) }
let(:used_by) { false }
diff --git a/spree/core/spec/models/spree/user_spec.rb b/spree/core/spec/models/spree/user_spec.rb
index 81bbaa0c82..db7b49b986 100644
--- a/spree/core/spec/models/spree/user_spec.rb
+++ b/spree/core/spec/models/spree/user_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'
-describe Spree::LegacyUser, type: :model do # rubocop:disable RSpec/MultipleDescribes
+describe Spree.customer_class, type: :model do # rubocop:disable RSpec/MultipleDescribes
it_behaves_like 'lifecycle events', factory: :user, event_prefix: 'user'
describe '#can_be_deleted?' do
diff --git a/spree/lib/generators/spree/install/install_generator.rb b/spree/lib/generators/spree/install/install_generator.rb
index 6b55d5d240..c1a2068a47 100644
--- a/spree/lib/generators/spree/install/install_generator.rb
+++ b/spree/lib/generators/spree/install/install_generator.rb
@@ -16,7 +16,7 @@ module Spree
class_option :admin_email, type: :string
class_option :admin_password, type: :string
class_option :enforce_available_locales, type: :boolean, default: nil
- class_option :authentication, type: :string, default: 'devise'
+ class_option :authentication, type: :string, default: nil
def self.source_paths
paths = superclass.source_paths
@@ -41,11 +41,7 @@ module Spree
end
def install_authentication
- if @authentication == 'devise'
- generate 'spree:authentication:devise'
- elsif @authentication == 'dummy'
- generate 'spree:authentication:dummy'
- end
+ generate 'spree:authentication:dummy' if @authentication == 'dummy'
end
def configure_application