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

Revert "Utilize Devise location helpers for redirecting" #232

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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