diff --git a/app/controllers/devise_controller.rb b/app/controllers/devise_controller.rb index 8603b938..376b03d4 100644 --- a/app/controllers/devise_controller.rb +++ b/app/controllers/devise_controller.rb @@ -181,12 +181,4 @@ MESSAGE format.any(*navigational_formats, &block) end end - - def request_format - @request_format ||= request.format.try(:ref) - end - - def is_navigational_format? - Devise.navigational_formats.include?(request_format) - end end diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 0c147b06..a83a77d0 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 @@ -262,6 +267,14 @@ module Devise super # call the default behaviour which resets the session end + def request_format + @request_format ||= request.format.try(:ref) + end + + def is_navigational_format? + Devise.navigational_formats.include?(request_format) + end + private def expire_devise_cached_variables! 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