Devis logout error ActionController::InvalidAuthenticityTokenon logout

78 Views Asked by At

Using Rails 6.0.3.6, ruby 2.7.2p137 and devise (4.7.3).

When user call a logout it works well, if its a normal scenario. But the same logout gives error in the conditions:

  1. If user has opened 2 tabs and logout from 1st tab and tries to logout from the 2nd tab it gives error.

  2. If user logout and click the browser back button and then again click the logout button then it again result in an error.

Please suggest an process to be coded in the project by which the user get redirected to the home path stating user is already signed out instead of giving the error.

Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 5ms (ActiveRecord: 0.5ms | Allocations: 3332)
  
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

Note: <%= csrf_meta_tags %> tag is already present in the layout of application.

1

There are 1 best solutions below

3
Sachin Singh On

Having a before_action :authenticate_user! in your application_controller.rb should solve the issue.

# application_controller.rb
class ApplicationController < ActionController::Base

  before_action :authenticate_user!
  
  ...
  ...
end