Skip to content

Commit dde9c6a

Browse files
Merge pull request #228 from cpfergus1/connorferguson/sol-306-deprecate-redirect_back_or_default
Utilize Devise location helpers for redirecting
2 parents b8b9ac7 + 51741d5 commit dde9c6a

File tree

6 files changed

+28
-17
lines changed

6 files changed

+28
-17
lines changed

lib/controllers/backend/spree/admin/user_sessions_controller.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def create
1717
respond_to do |format|
1818
format.html {
1919
flash[:success] = I18n.t('spree.logged_in_succesfully')
20-
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
20+
redirect_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
2121
}
2222
format.js {
2323
user = resource.record
@@ -47,9 +47,4 @@ def set_user_language_locale_key
4747
def accurate_title
4848
I18n.t('spree.login')
4949
end
50-
51-
def redirect_back_or_default(default)
52-
redirect_to(session["spree_user_return_to"] || default)
53-
session["spree_user_return_to"] = nil
54-
end
5550
end

lib/controllers/frontend/spree/user_sessions_controller.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def create
1919
respond_to do |format|
2020
format.html do
2121
flash[:success] = I18n.t('spree.logged_in_succesfully')
22-
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
22+
redirect_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
2323
end
2424
format.js { render success_json }
2525
end
@@ -49,11 +49,6 @@ def accurate_title
4949
I18n.t('spree.login')
5050
end
5151

52-
def redirect_back_or_default(default)
53-
redirect_to(session["spree_user_return_to"] || default)
54-
session["spree_user_return_to"] = nil
55-
end
56-
5752
def success_json
5853
{
5954
json: {

lib/controllers/frontend/spree/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def create
1717
session[:guest_token] = nil
1818
end
1919

20-
redirect_back_or_default(root_url)
20+
redirect_to stored_spree_user_location_or(root_url)
2121
else
2222
render :new
2323
end

lib/decorators/frontend/controllers/spree/checkout_controller_decorator.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def check_authorization
4545
def check_registration
4646
return unless registration_required?
4747

48-
store_location
4948
redirect_to spree.checkout_registration_path
5049
end
5150

lib/spree/auth/engine.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def self.prepare_backend
5959
redirect_to spree.admin_unauthorized_path
6060
end
6161
else
62-
store_location
6362

6463
if Spree::Auth::Engine.redirect_back_on_unauthorized?
6564
redirect_back(fallback_location: spree.admin_login_path)
@@ -70,7 +69,6 @@ def self.prepare_backend
7069
end
7170
end
7271

73-
7472
def self.prepare_frontend
7573
Spree::BaseController.unauthorized_redirect = -> do
7674
if spree_current_user
@@ -82,7 +80,6 @@ def self.prepare_frontend
8280
redirect_to spree.unauthorized_path
8381
end
8482
else
85-
store_location
8683

8784
if Spree::Auth::Engine.redirect_back_on_unauthorized?
8885
redirect_back(fallback_location: spree.login_path)

lib/spree/authentication_helpers.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,30 @@ def spree_current_user
2323
to: :spree,
2424
prefix: :spree
2525
end
26+
27+
private
28+
29+
def authenticate_spree_user!
30+
store_spree_user_location! if storable_spree_user_location?
31+
32+
super
33+
end
34+
35+
# It's important that the location is NOT stored if:
36+
# - The request method is not GET (non idempotent)
37+
# - The request is handled by a Devise controller such as Devise::SessionsController as that could cause an
38+
# infinite redirect loop.
39+
# - The request is an Ajax request as this can lead to very unexpected behaviour.
40+
def storable_spree_user_location?
41+
request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
42+
end
43+
44+
def store_spree_user_location!
45+
store_location_for(:spree_current_user, request.fullpath)
46+
end
47+
48+
def stored_spree_user_location_or(fallback_location)
49+
stored_location_for(:spree_current_user) || fallback_location
50+
end
2651
end
2752
end

0 commit comments

Comments
 (0)