Skip to content

Instantly share code, notes, and snippets.

@nakhli
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nakhli/dc7e062e950e556cc082 to your computer and use it in GitHub Desktop.
Save nakhli/dc7e062e950e556cc082 to your computer and use it in GitHub Desktop.
Fix AngularJS + Rails CSRF protection
ActiveSupport.on_load(:action_controller) do
after_action :set_csrf_cookie_for_angular_js
define_method :set_csrf_cookie_for_angular_js do
cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
end
private :set_csrf_cookie_for_angular_js
define_method :verified_request_with_angular_header? do
verified_request_without_angular_header? || form_authenticity_token == request.headers['X-XSRF-Token']
end
alias_method_chain :verified_request?, :angular_header
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment