Skip to content

Commit

Permalink
Merge pull request #232 from nebulab/revert-228-connorferguson/sol-30…
Browse files Browse the repository at this point in the history
…6-deprecate-redirect_back_or_default

Revert "Utilize Devise location helpers for redirecting"
  • Loading branch information
kennyadsl authored Sep 30, 2022
2 parents a8ddecf + ab5dccb commit e95fdcc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create
respond_to do |format|
format.html {
flash[:success] = I18n.t('spree.logged_in_succesfully')
redirect_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
}
format.js {
user = resource.record
Expand Down Expand Up @@ -47,4 +47,9 @@ def set_user_language_locale_key
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
end
7 changes: 6 additions & 1 deletion lib/controllers/frontend/spree/user_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create
respond_to do |format|
format.html do
flash[:success] = I18n.t('spree.logged_in_succesfully')
redirect_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
end
format.js { render success_json }
end
Expand Down Expand Up @@ -49,6 +49,11 @@ 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: {
Expand Down
2 changes: 1 addition & 1 deletion lib/controllers/frontend/spree/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def create
session[:guest_token] = nil
end

redirect_to stored_spree_user_location_or(root_url)
redirect_back_or_default(root_url)
else
render :new
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def check_authorization
def check_registration
return unless registration_required?

store_location
redirect_to spree.checkout_registration_path
end

Expand Down
3 changes: 3 additions & 0 deletions lib/spree/auth/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def self.prepare_backend
redirect_to spree.admin_unauthorized_path
end
else
store_location

if Spree::Auth::Engine.redirect_back_on_unauthorized?
redirect_back(fallback_location: spree.admin_login_path)
Expand All @@ -69,6 +70,7 @@ def self.prepare_backend
end
end


def self.prepare_frontend
Spree::BaseController.unauthorized_redirect = -> do
if spree_current_user
Expand All @@ -80,6 +82,7 @@ def self.prepare_frontend
redirect_to spree.unauthorized_path
end
else
store_location

if Spree::Auth::Engine.redirect_back_on_unauthorized?
redirect_back(fallback_location: spree.login_path)
Expand Down
25 changes: 0 additions & 25 deletions lib/spree/authentication_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,5 @@ def spree_current_user
to: :spree,
prefix: :spree
end

private

def authenticate_spree_user!
store_spree_user_location! if storable_spree_user_location?

super
end

# It's important that the location is NOT stored if:
# - The request method is not GET (non idempotent)
# - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an
# infinite redirect loop.
# - The request is an Ajax request as this can lead to very unexpected behaviour.
def storable_spree_user_location?
request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
end

def store_spree_user_location!
store_location_for(:spree_current_user, request.fullpath)
end

def stored_spree_user_location_or(fallback_location)
stored_location_for(:spree_current_user) || fallback_location
end
end
end

0 comments on commit e95fdcc

Please sign in to comment.