diff --git a/app/controllers/spree/user_confirmations_controller.rb b/app/controllers/spree/user_confirmations_controller.rb index 7615638a1..e58ad95e1 100644 --- a/app/controllers/spree/user_confirmations_controller.rb +++ b/app/controllers/spree/user_confirmations_controller.rb @@ -8,6 +8,18 @@ class Spree::UserConfirmationsController < Devise::ConfirmationsController before_action :set_current_order + # POST /resource/confirmation + def create + self.resource = resource_class.send_confirmation_instructions(resource_params, current_store) + yield resource if block_given? + + if successfully_sent?(resource) + respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name)) + else + respond_with(resource) + end + end + # GET /resource/confirmation?confirmation_token=abcdef def show self.resource = resource_class.confirm_by_token(params[:confirmation_token]) diff --git a/app/controllers/spree/user_registrations_controller.rb b/app/controllers/spree/user_registrations_controller.rb index 9bc8c44d7..d6188a81e 100644 --- a/app/controllers/spree/user_registrations_controller.rb +++ b/app/controllers/spree/user_registrations_controller.rb @@ -23,6 +23,7 @@ def new # POST /resource/sign_up def create @user = build_resource(spree_user_params) + resource.skip_confirmation_notification! if Spree::Auth::Config[:confirmable] resource_saved = resource.save yield resource if block_given? if resource_saved @@ -30,10 +31,12 @@ def create set_flash_message :notice, :signed_up sign_up(resource_name, resource) session[:spree_user_signup] = true + resource.send_confirmation_instructions(current_store) if Spree::Auth::Config[:confirmable] redirect_to_checkout_or_account_path(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" expire_data_after_sign_in! + resource.send_confirmation_instructions(current_store) if Spree::Auth::Config[:confirmable] respond_with resource, location: after_inactive_sign_up_path_for(resource) end else diff --git a/app/mailers/spree/user_mailer.rb b/app/mailers/spree/user_mailer.rb index 917d025f3..1b8883447 100644 --- a/app/mailers/spree/user_mailer.rb +++ b/app/mailers/spree/user_mailer.rb @@ -8,14 +8,16 @@ def reset_password_instructions(user, token, *_args) @edit_password_reset_url = spree.edit_spree_user_password_url(reset_password_token: token, host: @current_store.url) @user = user - mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :reset_password_instructions]) + mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :reset_password_instructions]), store_url: @current_store.url end def confirmation_instructions(user, token, _opts = {}) - @confirmation_url = spree.spree_user_confirmation_url(confirmation_token: token, host: Spree::Store.current.url) + current_store_id = _opts[:current_store_id] + @current_store = Spree::Store.find(current_store_id) || Spree::Store.current + @confirmation_url = spree_user_confirmation_url(confirmation_token: token, host: Spree::Store.current.url) @email = user.email - mail to: user.email, from: from_address, subject: Spree::Store.current.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :confirmation_instructions]) + mail to: user.email, from: from_address, subject: @current_store.name + ' ' + I18n.t(:subject, scope: [:devise, :mailer, :confirmation_instructions]), store_url: @current_store.url end end end diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 6c3993330..dc98944fe 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -27,6 +27,31 @@ def admin? has_spree_role?('admin') end + def self.send_confirmation_instructions(attributes = {}, current_store) + confirmable = find_by_unconfirmed_email_with_errors(attributes) if reconfirmable + unless confirmable.try(:persisted?) + confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found) + end + confirmable.resend_confirmation_instructions(current_store) if confirmable.persisted? + confirmable + end + + def resend_confirmation_instructions(current_store) + pending_any_confirmation do + send_confirmation_instructions(current_store) + end + end + + def send_confirmation_instructions(current_store) + unless @raw_confirmation_token + generate_confirmation_token! + end + + opts = pending_reconfirmation? ? { to: unconfirmed_email } : {} + opts[:current_store_id] = current_store.id + send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts) + end + def self.send_reset_password_instructions(attributes={}, current_store) recoverable = find_or_initialize_with_errors(reset_password_keys, attributes, :not_found) recoverable.send_reset_password_instructions(current_store) if recoverable.persisted? diff --git a/spec/features/confirmation_spec.rb b/spec/features/confirmation_spec.rb index ce9e95d16..41dbd202a 100644 --- a/spec/features/confirmation_spec.rb +++ b/spec/features/confirmation_spec.rb @@ -3,7 +3,7 @@ RSpec.feature 'Confirmation', type: :feature, reload_user: true do before do set_confirmable_option(true) - Spree::UserMailer.stub(:confirmation_instructions).and_return(double(deliver: true)) + expect(Spree::UserMailer).to receive(:confirmation_instructions).with(anything, anything, { current_store_id: Spree::Store.current.id }).and_return(double(deliver: true)) end after(:each) { set_confirmable_option(false) } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 0ef22b2f2..e16727b10 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -68,7 +68,7 @@ describe "confirmable", reload_user: true do it "is confirmable if the confirmable option is enabled" do set_confirmable_option(true) - Spree::UserMailer.stub(:confirmation_instructions).and_return(double(deliver: true)) + Spree::UserMailer.stub(:confirmation_instructions).with(anything, anything, { current_store_id: Spree::Store.current.id }).and_return(double(deliver: true)) expect(Spree.user_class.devise_modules).to include(:confirmable) set_confirmable_option(false) end