I am using Devise 3.2.0
for authentication and found an issue when I do the following:
- tab 1: sign in to app
- tab 2: go to any page in the app
- tab 2: sign out (success)
- tab 1: sign out (failure - see exception below)
Exception raised:
ActionController::InvalidAuthenticityToken in Devise::SessionsController#destroy
In the development log I see:
Can't verify CSRF token authenticity
And the top three lines of the stack trace are:
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
actionpack (4.0.0) lib/action_controller/metal/request_forgery_protection.rb:163:in `handle_unverified_request'
actionpack (4.0.0) lib/action_controller/metal/request_forgery_protection.rb:170:in `handle_unverified_request'
devise (3.2.0) lib/devise/controllers/helpers.rb:198:in `handle_unverified_request'
How can I ensure successive sign outs don't raise an exception?
If you are still having this issue as i did in
Rails 5
anddevise 4.4.1
, in the app/controllers/application_controller.rb changeto
hope it helps.
Here is whats happening,
When you initially signed out from tab 2, session and authenticity_token associated with the logged in user was destroyed. When you try to sign out from tab 1, Devise again tries to destroy the session using the authenticity_token which was destroyed on tab 2.
Hence, you get the error
ActionController::InvalidAuthenticityToken
as devise fails to authenticate using the givenauthenticity_token
.You only get one unique session per sign in, if that gets destroyed you'll have nothing to destroy again.
EDIT
This behavior is not provided by Devise. If you wish to implement such behavior you will have to override SessionsController.
Create a
sessions_controller.rb
file inapp/controllers/users
directoryUpdate
routes.rb
Kirti is exactly right. I've had this problem yesterday but with a custom authentication solution. If this is really a problem that you want to fix, you could figure out how to override Devise's signout action and add
skip_before_filter :verify_authenticity_token
for that action.past this in the layout: <%= csrf_meta_tags %>
This bug was fixed in
devise 3.3.0
.already_signed_out
inconfig/locales/en.yml
You can change strategy of verify csrf token.
In rails 3 the default strategy when verify is failed, is return a null session. In rails 4 was changed the strategy in application_controller to return a exception.
I solve this, changing in my application_controller.rb
This way, use the default strategy.