Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 6 - constant ActionController::InvalidAuthenticityToken

I'm tinkering with Rails 6 and I am constantly getting ActionController::InvalidAuthenticityToken on forms generated by rails, such as (implementing the rails tutorial book register/login flow)

<%= form_for(@user, url: 'signup') do |f| %>
     <%= render 'partials/error_messages' %>
     <%= f.label :name, "Nimi" %>
     <%= f.text_field :name %>
     <%= f.label :email, "E-mail" %>
     <%= f.email_field :email %>
     <%= f.label :password, "Parool" %>
     <%= f.password_field :password %>
     <%= f.label :password_confirmation, "Korda parooli" %>
     <%= f.password_field :password_confirmation %>
     <%= f.submit "Loo konto", class: "button-green" %>
<% end %>

this happens on all forms, and the output dumps look like this

Dumps

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Storebase - kaasaegsed e-poed!</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body class="bg-gray-100 text-gray-900">
    <% flash.each do |message_type, message| %>
      <div class="bg-blue-100 text-blue-500 flex items-center h-12 px-12 shadow-lg flash-<%= message_type %>"><%= message %></div>
    <% end %>

    <%= yield %>
    <%= debug(params) if Rails.env.development? %>
  </body>
</html>

What should I do?

like image 323
Rando Hinn Avatar asked Aug 20 '19 11:08

Rando Hinn


Video Answer


1 Answers

Was seeing this in a controller that subclasses Devise to get after action hooks. It was only happening on :destroy actions for me.

class MySessionsController < Devise::SessionsController
  after_action :after_login,  only: [:create]
  after_action :after_logout, only: [:destroy]

  private

  def after_login
  end

  def after_logout
  end

I don't see the risk in skipping authenticating on a destroy, so adding this line inside the controller fixed the issue for me: skip_before_action :verify_authenticity_token, only:[:destroy]. ¯_(ツ)_/¯

Is this a bug or feature introduced by the Rails6 upgrade? Is there a security risk that I'm not seeing by skipping? Any insights would be appreciated :)

like image 106
davidpm4 Avatar answered Oct 04 '22 19:10

davidpm4