Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InertiaRails Controller after action no cookie causes error #109

Open
EamonIndigoSpark opened this issue Dec 15, 2023 · 5 comments
Open

Comments

@EamonIndigoSpark
Copy link

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

@PedroAugustoRamalhoDuarte
Copy link
Contributor

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

@EamonIndigoSpark
Copy link
Author

EamonIndigoSpark commented Dec 18, 2023

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
Copy link
Contributor

@EamonIndigoSpark You accidentally share your app password.

@PedroAugustoRamalhoDuarte
Copy link
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
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants