Redirect to the previous URL on timeout, closes #1596
This commit is contained in:
parent
477d9fbcba
commit
275c480f89
|
@ -9,6 +9,7 @@ Notes: https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.
|
|||
* Do not run validations unless on reconfirmable branch
|
||||
|
||||
* enhancements
|
||||
* Redirect to the previous URL on timeout
|
||||
* Inherit from the same Devise parent controller (by @sj26)
|
||||
* Allow parent_controller to be customizable via Devise.parent_controller, useful for engines
|
||||
* Allow router_name to be customizable via Devise.router_name, useful for engines
|
||||
|
|
|
@ -53,14 +53,19 @@ module Devise
|
|||
|
||||
def redirect
|
||||
store_location!
|
||||
if flash[:timedout] && flash[:alert]
|
||||
flash.keep(:timedout)
|
||||
flash.keep(:alert)
|
||||
else
|
||||
flash[:alert] = i18n_message
|
||||
end
|
||||
redirect_to redirect_url
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def i18n_message(default = nil)
|
||||
message = warden.message || warden_options[:message] || default || :unauthenticated
|
||||
message = warden_message || default || :unauthenticated
|
||||
|
||||
if message.is_a?(Symbol)
|
||||
I18n.t(:"#{scope}.#{message}", :resource_name => scope,
|
||||
|
@ -71,6 +76,15 @@ module Devise
|
|||
end
|
||||
|
||||
def redirect_url
|
||||
if warden_message == :timeout
|
||||
flash[:timedout] = true
|
||||
attempted_path || scope_path
|
||||
else
|
||||
scope_path
|
||||
end
|
||||
end
|
||||
|
||||
def scope_path
|
||||
opts = {}
|
||||
route = :"new_#{scope}_session_path"
|
||||
opts[:format] = request_format unless skip_format?
|
||||
|
@ -139,6 +153,10 @@ module Devise
|
|||
env['warden.options']
|
||||
end
|
||||
|
||||
def warden_message
|
||||
@message ||= warden.message || warden_options[:message]
|
||||
end
|
||||
|
||||
def scope
|
||||
@scope ||= warden_options[:scope] || Devise.default_scope
|
||||
end
|
||||
|
|
|
@ -41,7 +41,7 @@ class SessionTimeoutTest < ActionController::IntegrationTest
|
|||
assert_not_nil last_request_at
|
||||
|
||||
get users_path
|
||||
assert_redirected_to new_user_session_path
|
||||
assert_redirected_to users_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
|
@ -68,12 +68,25 @@ class SessionTimeoutTest < ActionController::IntegrationTest
|
|||
|
||||
get expire_user_path(user)
|
||||
get users_path
|
||||
assert_redirected_to new_user_session_path
|
||||
assert_redirected_to users_path
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'error message with i18n' do
|
||||
store_translations :en, :devise => {
|
||||
:failure => { :user => { :timeout => 'Session expired!' } }
|
||||
} do
|
||||
user = sign_in_as_user
|
||||
|
||||
get expire_user_path(user)
|
||||
get root_path
|
||||
follow_redirect!
|
||||
assert_contain 'Session expired!'
|
||||
end
|
||||
end
|
||||
|
||||
test 'error message with i18n with double redirect' do
|
||||
store_translations :en, :devise => {
|
||||
:failure => { :user => { :timeout => 'Session expired!' } }
|
||||
} do
|
||||
|
@ -82,6 +95,7 @@ class SessionTimeoutTest < ActionController::IntegrationTest
|
|||
get expire_user_path(user)
|
||||
get users_path
|
||||
follow_redirect!
|
||||
follow_redirect!
|
||||
assert_contain 'Session expired!'
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue