Skip to content

Commit

Permalink
Add an option use_redirect_back_or_to_by_rails
Browse files Browse the repository at this point in the history
Fix Sorcery#296

`redirect_back_or_to` defined by Rails 7 conflict by name with Sorcery's 'redirect_back_or_to'.
This commit adds an option to set whether to override Sorcery's 'redirect_back_or_to' by Rails 7's 'redirect_back_or_to'.

ref: Sorcery#296 (comment)
  • Loading branch information
atolix committed Apr 24, 2024
1 parent bbebb0f commit c65919f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/generators/sorcery/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
#
# config.save_return_to_url =

# Set whether to override Sorcery's 'redirect_back_or_to' by Rails 7's 'redirect_back_or_to'.
# Rails 7 released a new method called 'redirect_back_or_to' as a replacement for 'redirect_back'.
# That may conflict with the method by the same name defined by Sorcery.
# If you set this option to true, Sorcery's redirect_back_or_to will be overridden by
# the method of the same name defined in Rails 7.
# Default: `false`
#
# config.use_redirect_back_or_to_by_rails =

# Set domain option for cookies; Useful for remember_me submodule.
# Default: `nil`
#
Expand Down Expand Up @@ -226,7 +235,7 @@
# config.line.bot_prompt = "normal"
# config.line.user_info_mapping = {name: 'displayName'}


# For information about Discord API
# https://discordapp.com/developers/docs/topics/oauth2
# config.discord.key = "xxxxxx"
Expand Down
9 changes: 7 additions & 2 deletions lib/sorcery/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ def current_user=(user)
# used when a user tries to access a page while logged out, is asked to login,
# and we want to return him back to the page he originally wanted.
def redirect_back_or_to(url, flash_hash = {})
redirect_to(session[:return_to_url] || url, flash: flash_hash)
session[:return_to_url] = nil
if Config.use_redirect_back_or_to_by_rails
super(request.referer)
else
warn('[WARNING] `redirect_back_or_to` overrides the method of the same name defined in Rails 7. If you want to avoid the override, you can set `config.use_redirect_back_or_to_by_rails = true`.')
redirect_to(session[:return_to_url] || url, flash: flash_hash)
session[:return_to_url] = nil
end
end

# The default action for denying non-authenticated users.
Expand Down
6 changes: 5 additions & 1 deletion lib/sorcery/controller/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class << self
attr_accessor :after_logout
attr_accessor :after_remember_me

# set whether to override Sorcery's 'redirect_back_or_to' by Rails 7's 'redirect_back_or_to'.
attr_accessor :use_redirect_back_or_to_by_rails

def init!
@defaults = {
:@user_class => nil,
Expand All @@ -32,7 +35,8 @@ def init!
:@after_logout => Set.new,
:@after_remember_me => Set.new,
:@save_return_to_url => true,
:@cookie_domain => nil
:@cookie_domain => nil,
:@use_redirect_back_or_to_by_rails => false
}
end

Expand Down
34 changes: 34 additions & 0 deletions spec/controllers/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

expect(Sorcery::Controller::Config.not_authenticated_action).to eq :my_action
end

it "enables configuration option 'use_redirect_back_or_to_by_rails'" do
sorcery_controller_property_set(:use_redirect_back_or_to_by_rails, true)

expect(Sorcery::Controller::Config.use_redirect_back_or_to_by_rails).to be true
end
end

# ----------------- PLUGIN ACTIVATED -----------------------
Expand Down Expand Up @@ -196,5 +202,33 @@

expect(assigns[:result]).to eq user
end

describe 'redirect_back_or_to' do
describe 'use_redirect_back_or_to_by_rails' do
context 'when true' do
before do
sorcery_controller_property_set(:use_redirect_back_or_to_by_rails, true)
allow_any_instance_of(ActionController::TestRequest).to receive(:referer).and_return('http://test.host/referer_action')
end

it 'uses Rails 7 redirect_back_or_to method' do
get :test_return_to

expect(response).to redirect_to('http://test.host/referer_action')
end
end

context 'when false' do
before { sorcery_controller_property_set(:use_redirect_back_or_to_by_rails, false) }

it 'uses Sorcery redirect_back_or_to method' do
session[:return_to_url] = 'http://test.host/some_action'
get :test_return_to

expect(response).to redirect_to('http://test.host/some_action')
end
end
end
end
end
end

0 comments on commit c65919f

Please sign in to comment.