Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy routes/controllers/views/tests from auth_devise #172

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
969f182
[BREAKS BUILD] Copy feature specs from solidus_auth_devise
gsmendoza Jul 27, 2021
f727619
Remove backend solidus_auth_devise feature specs
gsmendoza Jul 27, 2021
ff667d5
Remove skipped spec
gsmendoza Jul 27, 2021
1b35df8
Copy solidus_auth_devise frontend routes
gsmendoza Jul 27, 2021
e34e9e6
Move checkout solidus_auth_devise routes to checkout section
gsmendoza Jul 27, 2021
6245ee4
Add solidus_auth_devise
gsmendoza Jul 27, 2021
544e8d0
Copy solidus_auth_devise controllers
gsmendoza Jul 27, 2021
7ba6cbf
Remove frontend_available? monkeypatch
gsmendoza Jul 27, 2021
479172f
Qualify Password field identifiers
gsmendoza Jul 27, 2021
5d2db51
Move views from lib to app
gsmendoza Jul 27, 2021
9b287a7
Update Edit Account form field identifiers
gsmendoza Jul 27, 2021
f86dd85
Inline solidus_auth_devise CheckoutControllerDecorator to CheckoutCon…
gsmendoza Jul 27, 2021
8af71c1
Remove overridden checkout controller methods
gsmendoza Jul 27, 2021
75f0cf4
Copy FillAddressFields
gsmendoza Jul 27, 2021
30d1119
Copy solidus_auth_devise UserMailer with views
gsmendoza Jul 27, 2021
7a73d89
Fix spec "allow a user to register during checkout"
gsmendoza Jul 27, 2021
bc668ce
Fix "Sign In let a user sign in successfully" spec
gsmendoza Jul 27, 2021
c873a10
Copy unauthorized_redirect from solidus_auth_devise
gsmendoza Jul 27, 2021
f9e1b49
Fix 'allow a signed in user to logout' spec
gsmendoza Jul 27, 2021
3828604
Fix system specs that require checking out as guest
gsmendoza Jul 28, 2021
b5e0ebb
Fix expected completion routes in checkout specs
gsmendoza Jul 28, 2021
e00751a
Update specs for unauthorized order requests
gsmendoza Jul 28, 2021
4ec315b
Convert authentication feature specs to system specs
gsmendoza Jul 28, 2021
4165a32
Include Devise::Test::ControllerHelpers in controller spec
gsmendoza Jul 28, 2021
58dc7a8
[FIXES TEST SUITE] Sign in to request specs using Devise::Test::Integ…
gsmendoza Jul 28, 2021
0940006
Inline inclusion of Taxonomies to auth_devise controllers
gsmendoza Jul 28, 2021
40cb474
Ignore solidus_auth_devise controllers on solidus_compare
gsmendoza Jul 29, 2021
70c3713
Remove auth_views from solidus_compare
gsmendoza Jul 29, 2021
774c6b7
Move unauthorized_redirect handler to initializer
gsmendoza Jul 29, 2021
22f0bf5
Fix generator to copy solidus_auth_devise frontend files to app
gsmendoza Jul 29, 2021
4b1f2ba
Indicate that solidus_auth_devise is currently installed by the gener…
gsmendoza Jul 29, 2021
7387c68
Copy controller and mailer specs from solidus_auth_devise
gsmendoza Aug 2, 2021
a88dee4
Fix intermittent checkout example
gsmendoza Aug 5, 2021
bccabef
Update instructions to install Solidus with Auth Devise
gsmendoza Aug 6, 2021
9746326
Update comment in checkout controller
Aug 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ Just run:
rails new store --skip-javascript
cd store
bundle add solidus_core solidus_backend solidus_api solidus_sample
bin/rails generate solidus:install
bin/rails generate solidus:install --auto-accept
```

And type `y` when prompted if you want to install Solidus Auth Devise
Please note that `--auto-accept` will add [Solidus Auth Devise]
(https://github.com/solidusio/solidus_auth_devise) to your application. At the
moment, SolidusStarterFrontend requires the application to include the gem. In
the future, we'll make Solidus Auth Devise optional.

### For existing stores

Expand All @@ -61,6 +64,10 @@ gem 'solidus_sample'
And replace all the references of the string `Spree::Frontend::Config` in your
project with `SolidusStarterFrontend::Config`.

You'll also need to make sure that [Solidus Auth Devise]
(https://github.com/solidusio/solidus_auth_devise) is installed in your
application.

### Frontend installation

You can copy the starter frontend files to your project:
Expand All @@ -74,7 +81,11 @@ These commands will install the gem globally and copy this project's views,
assets, routes and controllers to your project. You can change easily anything
that we created; this gives you a lot of freedom of customization.

Please note however that you won't be able to auto-update the storefront code
In addition, please note that the command will add Solidus Auth Devise
frontend components to your app. At the moment, you will need to manually
remove the gem and its frontend components if you don't need them.

Finally, please note that you won't be able to auto-update the storefront code
with the next versions released since this project's gem will not be present in
your Gemfile.

Expand Down
16 changes: 0 additions & 16 deletions app/controllers/concerns/solidus_starter_frontend/auth_views.rb

This file was deleted.

72 changes: 63 additions & 9 deletions app/controllers/spree/checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ class CheckoutController < Spree::StoreController
before_action :ensure_valid_state

before_action :associate_user
before_action :check_registration, except: [:registration, :update_registration]
before_action :check_authorization

before_action :setup_for_current_state, only: [:edit, :update]

# This action builds some associations on the order, ex. addresses, which we
# don't need to build or save here.
skip_before_action :setup_for_current_state, only: [:registration, :update_registration]

helper 'spree/orders'

rescue_from Spree::Core::GatewayError, with: :rescue_from_spree_gateway_error
Expand Down Expand Up @@ -47,6 +52,20 @@ def update
end
end

def registration
@user = Spree::User.new
end

def update_registration
if params[:order][:email] =~ Devise.email_regexp && current_order.update(email: params[:order][:email])
gsmendoza marked this conversation as resolved.
Show resolved Hide resolved
redirect_to spree.checkout_path
else
flash[:registration_error] = t(:email_is_invalid, scope: [:errors, :messages])
@user = Spree::User.new
render 'registration'
end
end

private

def update_order
Expand Down Expand Up @@ -173,11 +192,6 @@ def ensure_sufficient_stock_lines
end
end

# Provides a route to redirect after order completion
def completion_route
spree.order_path(@order)
end

def setup_for_current_state
method_name = :"before_#{@order.state}"
send(method_name) if respond_to?(method_name, true)
Expand Down Expand Up @@ -220,10 +234,6 @@ def rescue_from_spree_gateway_error(exception)
render :edit
end

def check_authorization
authorize!(:edit, current_order, cookies.signed[:guest_token])
end

def insufficient_stock_error
packages = @order.shipments.map(&:to_package)
if packages.empty?
Expand All @@ -240,5 +250,49 @@ def insufficient_stock_error
end
end
end

def order_params
params.
fetch(:order, {}).
permit(:email)
end

def skip_state_validation?
%w(registration update_registration).include?(params[:action])
end

def check_authorization
authorize!(:edit, current_order, cookies.signed[:guest_token])
end

# Introduces a registration step whenever the +registration_step+ preference is true.
def check_registration
return unless registration_required?

store_location
redirect_to spree.checkout_registration_path
end

def registration_required?
Spree::Auth::Config[:registration_step] &&
!already_registered?
end

def already_registered?
spree_current_user || guest_authenticated?
end

def guest_authenticated?
current_order&.email.present? &&
Spree::Config[:allow_guest_checkout]
end

# Overrides the equivalent method defined in Spree::Core. This variation of the method will ensure that users
# are redirected to the tokenized order url unless authenticated as a registered user.
def completion_route
return spree.order_path(@order) if spree_current_user

spree.token_order_path(@order, @order.guest_token)
end
end
end
17 changes: 17 additions & 0 deletions app/controllers/spree/user_confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class Spree::UserConfirmationsController < Devise::ConfirmationsController
helper 'spree/base', 'spree/store'

include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Common
include Spree::Core::ControllerHelpers::Order
include Spree::Core::ControllerHelpers::Store
include SolidusStarterFrontend::Taxonomies

protected

def after_confirmation_path_for(resource_name, resource)
signed_in?(resource_name) ? spree.signed_in_root_path(resource) : spree.login_path
end
end
54 changes: 54 additions & 0 deletions app/controllers/spree/user_passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

class Spree::UserPasswordsController < Devise::PasswordsController
helper 'spree/base', 'spree/store'

include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Common
include Spree::Core::ControllerHelpers::Order
include Spree::Core::ControllerHelpers::Store
include SolidusStarterFrontend::Taxonomies

# Overridden due to bug in Devise.
# respond_with resource, location: new_session_path(resource_name)
# is generating bad url /session/new.user
#
# overridden to:
# respond_with resource, location: spree.login_path
#
def create
self.resource = resource_class.send_reset_password_instructions(params[resource_name])

set_flash_message(:notice, :send_instructions) if is_navigational_format?

if resource.errors.empty?
respond_with resource, location: spree.login_path
else
respond_with_navigational(resource) { render :new }
end
end

# Devise::PasswordsController allows for blank passwords.
# Silly Devise::PasswordsController!
# Fixes spree/spree#2190.
def update
if params[:spree_user][:password].blank?
self.resource = resource_class.new
resource.reset_password_token = params[:spree_user][:reset_password_token]
set_flash_message(:error, :cannot_be_blank)
render :edit
else
super
end
end

protected

def translation_scope
'devise.user_passwords'
end

def new_session_path(resource_name)
spree.send("new_#{resource_name}_session_path")
end
end
45 changes: 45 additions & 0 deletions app/controllers/spree/user_registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class Spree::UserRegistrationsController < Devise::RegistrationsController
helper 'spree/base', 'spree/store'

include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Common
include Spree::Core::ControllerHelpers::Order
include Spree::Core::ControllerHelpers::Store
include SolidusStarterFrontend::Taxonomies

before_action :check_permissions, only: [:edit, :update]
skip_before_action :require_no_authentication

def create
build_resource(spree_user_params)
if resource.save
set_flash_message(:notice, :signed_up)
sign_in(:spree_user, resource)
session[:spree_user_signup] = true
respond_with resource, location: after_sign_up_path_for(resource)
else
clean_up_passwords(resource)
respond_with(resource) do |format|
format.html { render :new }
end
end
end

protected

def translation_scope
'devise.user_registrations'
end

def check_permissions
authorize!(:create, resource)
end

private

def spree_user_params
params.require(:spree_user).permit(Spree::PermittedAttributes.user_attributes | [:email])
end
end
67 changes: 67 additions & 0 deletions app/controllers/spree/user_sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

class Spree::UserSessionsController < Devise::SessionsController
helper 'spree/base', 'spree/store'

include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Common
include Spree::Core::ControllerHelpers::Order
include Spree::Core::ControllerHelpers::Store
include SolidusStarterFrontend::Taxonomies

# This is included in ControllerHelpers::Order. We just want to call
# it after someone has successfully logged in.
after_action :set_current_order, only: :create

def create
authenticate_spree_user!

if spree_user_signed_in?
respond_to do |format|
format.html do
flash[:success] = I18n.t('spree.logged_in_succesfully')
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
end
format.js { render success_json }
end
else
respond_to do |format|
format.html do
flash.now[:error] = t('devise.failure.invalid')
render :new
end
format.js do
render json: { error: t('devise.failure.invalid') },
status: :unprocessable_entity
end
end
end
end

protected

def translation_scope
'devise.user_sessions'
end

private

def accurate_title
I18n.t('spree.login')
end

def redirect_back_or_default(default)
redirect_to(session["spree_user_return_to"] || default)
session["spree_user_return_to"] = nil
end

def success_json
{
json: {
user: spree_current_user,
ship_address: spree_current_user.ship_address,
bill_address: spree_current_user.bill_address
}.to_json
}
end
end
Loading