Skip to content

Commit

Permalink
Remove #redirect_back_or_default
Browse files Browse the repository at this point in the history
With the deprecation of #redirect_back_or_default in solidus 4.0,
we can utilize Devise helpers store_location_for and stored_location_for
to provide the same functionality.

Co-Authored-By: Elia Schito <[email protected]>
  • Loading branch information
cpfergus1 and elia committed Sep 13, 2022
1 parent bb291bd commit 22bd2ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 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_back_or_default(after_sign_in_path_for(spree_current_user))
redirect_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
}
format.js {
user = resource.record
Expand Down Expand Up @@ -47,9 +47,4 @@ 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: 1 addition & 6 deletions 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_back_or_default(after_sign_in_path_for(spree_current_user))
redirect_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
end
format.js { render success_json }
end
Expand Down Expand Up @@ -49,11 +49,6 @@ 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_back_or_default(root_url)
redirect_to stored_spree_user_location_or(root_url)
else
render :new
end
Expand Down
25 changes: 25 additions & 0 deletions lib/spree/authentication_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,30 @@ 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 22bd2ce

Please sign in to comment.