diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 0c147b06..23c4c503 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -162,8 +162,8 @@ module Devise users.any? end - # Returns and delete the url stored in the session for the given scope. Useful - # for giving redirect backs after sign up: + # Returns and delete (if it's navigational format) the url stored in the session for + # the given scope. Useful for giving redirect backs after sign up: # # Example: # @@ -171,7 +171,12 @@ module Devise # def stored_location_for(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) - session.delete("#{scope}_return_to") + + if is_navigational_format? + session.delete("#{scope}_return_to") + else + session["#{scope}_return_to"] + end end # The scope root url to be used when he's signed in. By default, it first diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index 3818d9cc..7af0a686 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -13,6 +13,34 @@ class SessionsControllerTest < ActionController::TestCase end end + test "#create delete the url stored in the session if the requested format is navigational" do + request.env["devise.mapping"] = Devise.mappings[:user] + request.session["user_return_to"] = 'foo.bar' + + user = create_user + user.confirm! + post :create, :user => { + :email => user.email, + :password => user.password + } + + assert_nil request.session["user_return_to"] + end + + test "#create doesn't delete the url stored in the session if the requested format is not navigational" do + request.env["devise.mapping"] = Devise.mappings[:user] + request.session["user_return_to"] = 'foo.bar' + + user = create_user + user.confirm! + post :create, :format => 'json', :user => { + :email => user.email, + :password => user.password + } + + assert_equal 'foo.bar', request.session["user_return_to"] + end + test "#create doesn't raise exception after Warden authentication fails when TestHelpers included" do request.env["devise.mapping"] = Devise.mappings[:user] post :create, :user => { @@ -54,4 +82,4 @@ class SessionsControllerTest < ActionController::TestCase end end end -end \ No newline at end of file +end