Last active
August 29, 2015 14:08
-
-
Save nakhli/dc7e062e950e556cc082 to your computer and use it in GitHub Desktop.
Fix AngularJS + Rails CSRF protection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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