Skip to content

InertiaRails Controller after action no cookie causes error #109

Open
@EamonIndigoSpark

Description

@EamonIndigoSpark

In the after_action I get an error, where the cookie is null:

require_relative "inertia_rails"
require_relative "helper"

module InertiaRails
module Controller
extend ActiveSupport::Concern

included do
  before_action do
    # :inertia_errors are deleted from the session by the middleware
    InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
  end
  helper ::InertiaRails::Helper

  after_action do
    cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia? || !protect_against_forgery?
  end
end

cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia? || !protect_against_forgery?

Resulting in this error

undefined method `[]=' for nil:NilClass

Activity

PedroAugustoRamalhoDuarte

PedroAugustoRamalhoDuarte commented on Dec 16, 2023

@PedroAugustoRamalhoDuarte
Contributor

Hello @EamonIndigoSpark, can you share your ApplicationController? Maybe cookies it not defined because you are not using cookies rails middleware

EamonIndigoSpark

EamonIndigoSpark commented on Dec 18, 2023

@EamonIndigoSpark
Author

Hi @PedroAugustoRamalhoDuarte
Thank you for your help, below is the contents of the controller I am using

`require "will_paginate/array"
class Cms::ApplicationController < ActionController::Base

protect_from_forgery

layout :set_layout
before_action :authenticate
before_action :set_site
before_action :set_current_org
before_action :show_current_org_logo
before_action :set_cms_for_menu
before_action :check_user_disabled

cache_sweeper :user_stamp_sweeper

VALID_CMS_HOSTNAMES = ['conferencestop']

def set_current_org
if user_signed_in? && (current_user.is_a_client? || current_user.is_a_supplier?)
if current_user.contact.present?
@current_org = current_user.contact.parent
end
if @current_org.blank? && !current_user.is_a_supplier?
flash[:alert] = "Sorry we could not find your organisation. Please contact Servace"
redirect_to destroy_user_session_path()
end
end

end

def authenticate
return true if (ENV["APP_NAME"] =~ /pentest/ ) != nil # don't need this when pentesting

end

def set_cms_for_menu
@cms = true
end

def set_layout
'integrated_layout'
end

def hide_main_nav
@hide_main_nav = true
end

private

def set_site

  @site_name = "conference_stop"
  @site = Site.find_or_create_by(name: "conference_stop")
  @body_class = "cs"
  session[:site_name] = "cs"

end


def check_user_disabled
  if user_signed_in? && current_user.disabled?
    flash[:alert] = "sorry your account has been suspended. Please contact us."
    redirect_to destroy_user_session_path
    return false
  end
end

def show_current_org_logo
 if current_user && current_user.is_a_client?
  @show_current_org_logo = true
 end
end

end

`

PedroAugustoRamalhoDuarte

PedroAugustoRamalhoDuarte commented on Dec 18, 2023

@PedroAugustoRamalhoDuarte
Contributor

@EamonIndigoSpark You accidentally share your app password.

PedroAugustoRamalhoDuarte

PedroAugustoRamalhoDuarte commented on Dec 18, 2023

@PedroAugustoRamalhoDuarte
Contributor

Your ApplicationController looks right its a very strange error, things you can check:

  • if the cookies is enable in the ruby on rails APP.
  • if you are using a rails only API (I dont think so).

For now you can downgrade de inertia rails version to: 3.0.0, this was the last update without XSRF-TOKEN update

EamonIndigoSpark

EamonIndigoSpark commented on Dec 18, 2023

@EamonIndigoSpark
Author

@PedroAugustoRamalhoDuarte Hi Pedro,
Thank you for your help, I will try the things you suggest, also I've removed the password (fortunately, it is only available internally)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

      Participants

      @PedroAugustoRamalhoDuarte@EamonIndigoSpark

      Issue actions

        InertiaRails Controller after action no cookie causes error · Issue #109 · inertiajs/inertia-rails