Skip to content
Open
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
11 changes: 10 additions & 1 deletion lib/generators/sorcery/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@
# config.not_authenticated_action =

# When a non logged-in user tries to enter a page that requires login, save
# the URL he wants to reach, and send him there after login, using 'redirect_back_or_to'.
# the URL he wants to reach, and send him there after login, using 'redirect_to_before_login_path'.
# Default: `true`
#
# config.save_return_to_url =

# Set whether to use 'redirect_back_or_to' defined in Rails 7.
# 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' calls 'super' to use
# 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
11 changes: 10 additions & 1 deletion lib/sorcery/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,16 @@ 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 = {})
def redirect_back_or_to(...)
if Config.use_redirect_back_or_to_by_rails
Copy link
Member

@willnet willnet May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using Rails 6.1, calling super will cause an exception. Therefore, I want to ensure that super is not called even if Config.use_redirect_back_or_to_by_rails returns true.

Can you update the tests to expect a NoMethodError exception when using Rails 6.1?

The tests are currently failing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed in 7a494fc

super
else
warn('[WARNING] `redirect_back_or_to` overrides the method of the same name defined in Rails 7. If you want to avoid overriding, you can set `config.use_redirect_back_or_to_by_rails = true` and use `redirect_to_before_login_path`. In a future version, `config.use_redirect_back_or_to_by_rails = true` will become the default.')
redirect_to_before_login_path(...)
end
end

def redirect_to_before_login_path(url, flash_hash = {})
redirect_to(session[:return_to_url] || url, flash: flash_hash)
session[:return_to_url] = nil
end
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 use 'redirect_back_or_to' defined in Rails 7.
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
37 changes: 37 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 @@ -186,5 +192,36 @@

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

context 'when Rails::VERSION::MAJOR >= 7', skip: Rails::VERSION::MAJOR < 7 do
it 'uses Rails 7 redirect_back_or_to method' do
get :test_redirect_back_or_to

expect(response).to redirect_to('http://test.host/referer_action')
end
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_redirect_back_or_to

expect(response).to redirect_to('http://test.host/some_action')
expect(flash[:notice]).to eq 'haha!'
end
end
end
end
end
end
39 changes: 22 additions & 17 deletions spec/rails_app/app/controllers/sorcery_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def test_auto_login
end

def test_return_to
@user = login(params[:email], params[:password])
redirect_to_before_login_path(:index, notice: 'haha!')
end

def test_redirect_back_or_to
@user = login(params[:email], params[:password])
redirect_back_or_to(:index, notice: 'haha!')
end
Expand Down Expand Up @@ -314,15 +319,15 @@ def test_login_from_battlenet

def test_return_to_with_external_twitter
if (@user = login_from(:twitter))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_jira
if (@user = login_from(:jira))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
Expand All @@ -332,119 +337,119 @@ def test_return_to_with_external_jira

def test_return_to_with_external_facebook
if (@user = login_from(:facebook))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_github
if (@user = login_from(:github))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_paypal
if (@user = login_from(:paypal))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_wechat
if (@user = login_from(:wechat))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_microsoft
if (@user = login_from(:microsoft))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_google
if (@user = login_from(:google))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_liveid
if (@user = login_from(:liveid))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_vk
if (@user = login_from(:vk))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_salesforce
if (@user = login_from(:salesforce))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_slack
if (@user = login_from(:slack))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_instagram
if (@user = login_from(:instagram))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_auth0
if (@user = login_from(:auth0))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_line
if @user = login_from(:line)
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_discord
if (@user = login_from(:discord))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
end

def test_return_to_with_external_battlenet
if (@user = login_from(:battlenet))
redirect_back_or_to 'bla', notice: 'Success!'
redirect_to_before_login_path 'bla', notice: 'Success!'
else
redirect_to 'blu', alert: 'Failed!'
end
Expand Down
1 change: 1 addition & 0 deletions spec/rails_app/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get :test_logout
get :some_action
post :test_return_to
post :test_redirect_back_or_to
get :test_auto_login
post :test_login_with_remember_in_login
get :test_login_from_cookie
Expand Down