Skip to content

Commit

Permalink
Merge pull request #270 from banyan/redirect_back_or_to-work-with-ext…
Browse files Browse the repository at this point in the history
…ernal

fix `redirect_back_or_to` when we login with externals
  • Loading branch information
NoamB committed Apr 22, 2012
2 parents 0ecc67f + 38d285f commit 949e710
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/sorcery/controller/submodules/external.rb
Expand Up @@ -46,7 +46,9 @@ def login_from(provider)
@provider.process_callback(params,session)
@user_hash = @provider.get_user_hash
if user = user_class.load_from_provider(provider,@user_hash[:uid].to_s)
return_to_url = session[:return_to_url]
reset_session
session[:return_to_url] = return_to_url
auto_login(user)
user
end
Expand Down
40 changes: 40 additions & 0 deletions spec/rails3/app/controllers/application_controller.rb
Expand Up @@ -137,6 +137,46 @@ def test_login_from5
end
end

def test_return_to_with_external
if @user = login_from(:twitter)
redirect_back_or_to "bla", :notice => "Success!"
else
redirect_to "blu", :alert => "Failed!"
end
end

def test_return_to_with_external2
if @user = login_from(:facebook)
redirect_back_or_to "bla", :notice => "Success!"
else
redirect_to "blu", :alert => "Failed!"
end
end

def test_return_to_with_external3
if @user = login_from(:github)
redirect_back_or_to "bla", :notice => "Success!"
else
redirect_to "blu", :alert => "Failed!"
end
end

def test_return_to_with_external4
if @user = login_from(:google)
redirect_back_or_to "bla", :notice => "Success!"
else
redirect_to "blu", :alert => "Failed!"
end
end

def test_return_to_with_external5
if @user = login_from(:liveid)
redirect_back_or_to "bla", :notice => "Success!"
else
redirect_to "blu", :alert => "Failed!"
end
end

def test_create_from_provider
provider = params[:provider]
login_from(provider)
Expand Down
32 changes: 32 additions & 0 deletions spec/rails3/spec/controller_oauth2_spec.rb
Expand Up @@ -81,6 +81,14 @@ def stub_all_oauth2_requests!
flash[:alert].should == "Failed!"
end

it "on successful login_from the user should be redirected to the url he originally wanted" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:facebook)
get :test_return_to_with_external2, {}, :return_to_url => "fuu"
response.should redirect_to("fuu")
flash[:notice].should == "Success!"
end

# provider: github
it "login_at redirects correctly (github)" do
create_new_user
Expand All @@ -103,6 +111,14 @@ def stub_all_oauth2_requests!
flash[:alert].should == "Failed!"
end

it "on successful login_from the user should be redirected to the url he originally wanted (github)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:github)
get :test_return_to_with_external3, {}, :return_to_url => "fuu"
response.should redirect_to("fuu")
flash[:notice].should == "Success!"
end

# provider: google
it "login_at redirects correctly (google)" do
create_new_user
Expand All @@ -125,6 +141,14 @@ def stub_all_oauth2_requests!
flash[:alert].should == "Failed!"
end

it "on successful login_from the user should be redirected to the url he originally wanted (google)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:google)
get :test_return_to_with_external4, {}, :return_to_url => "fuu"
response.should redirect_to("fuu")
flash[:notice].should == "Success!"
end

# provider: liveid
it "login_at redirects correctly (liveid)" do
create_new_user
Expand All @@ -147,6 +171,14 @@ def stub_all_oauth2_requests!
flash[:alert].should == "Failed!"
end

it "on successful login_from the user should be redirected to the url he originally wanted (liveid)" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:liveid)
get :test_return_to_with_external5, {}, :return_to_url => "fuu"
response.should redirect_to("fuu")
flash[:notice].should == "Success!"
end

end


Expand Down
9 changes: 9 additions & 0 deletions spec/rails3/spec/controller_oauth_spec.rb
Expand Up @@ -63,6 +63,15 @@ def stub_all_oauth_requests!
get :test_login_from, :oauth_verifier => "blablaRERASDFcxvSDFA"
flash[:alert].should == "Failed!"
end

it "on successful 'login_from' the user should be redirected to the url he originally wanted" do
sorcery_model_property_set(:authentications_class, Authentication)
create_new_external_user(:twitter)
get :test_return_to_with_external, {}, :return_to_url => "fuu"
response.should redirect_to("fuu")
flash[:notice].should == "Success!"
end

end

describe ApplicationController do
Expand Down

0 comments on commit 949e710

Please sign in to comment.