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

make CSRF protection work out of the box (#71) #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/inertia_rails.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'inertia_rails/renderer'
require 'inertia_rails/engine'

require 'patches/action_controller'
require 'patches/debug_exceptions'
require 'patches/better_errors'
require 'patches/request'
Expand Down
5 changes: 5 additions & 0 deletions lib/inertia_rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ module Controller
# :inertia_errors are deleted from the session by the middleware
InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
end

after_action do
# Axios by default looks for an XSRF-TOKEN cookie to use for POST requests
cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia?
end
end

module ClassMethods
Expand Down
8 changes: 8 additions & 0 deletions lib/patches/action_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module ActionController
module RequestForgeryProtection
private
def request_authenticity_tokens
[form_authenticity_param, request.x_csrf_token, request.headers['X-XSRF-TOKEN']]
end
end
end