Skip to content

Commit e95fdcc

Browse files
authored
Merge pull request #232 from nebulab/revert-228-connorferguson/sol-306-deprecate-redirect_back_or_default
Revert "Utilize Devise location helpers for redirecting"
2 parents a8ddecf + ab5dccb commit e95fdcc

File tree

6 files changed

+17
-28
lines changed

6 files changed

+17
-28
lines changed

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

Lines changed: 6 additions & 1 deletion
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_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
20+
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
2121
}
2222
format.js {
2323
user = resource.record
@@ -47,4 +47,9 @@ 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
5055
end

lib/controllers/frontend/spree/user_sessions_controller.rb

Lines changed: 6 additions & 1 deletion
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_to stored_spree_user_location_or(after_sign_in_path_for(spree_current_user))
22+
redirect_back_or_default(after_sign_in_path_for(spree_current_user))
2323
end
2424
format.js { render success_json }
2525
end
@@ -49,6 +49,11 @@ 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+
5257
def success_json
5358
{
5459
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_to stored_spree_user_location_or(root_url)
20+
redirect_back_or_default(root_url)
2121
else
2222
render :new
2323
end

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

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

48+
store_location
4849
redirect_to spree.checkout_registration_path
4950
end
5051

lib/spree/auth/engine.rb

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

6364
if Spree::Auth::Engine.redirect_back_on_unauthorized?
6465
redirect_back(fallback_location: spree.admin_login_path)
@@ -69,6 +70,7 @@ def self.prepare_backend
6970
end
7071
end
7172

73+
7274
def self.prepare_frontend
7375
Spree::BaseController.unauthorized_redirect = -> do
7476
if spree_current_user
@@ -80,6 +82,7 @@ def self.prepare_frontend
8082
redirect_to spree.unauthorized_path
8183
end
8284
else
85+
store_location
8386

8487
if Spree::Auth::Engine.redirect_back_on_unauthorized?
8588
redirect_back(fallback_location: spree.login_path)

lib/spree/authentication_helpers.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,5 @@ 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
5126
end
5227
end

0 commit comments

Comments
 (0)