Skip to content

Instantly share code, notes, and snippets.

@skwp
Created August 20, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save skwp/130d6b18ee90c1c93799 to your computer and use it in GitHub Desktop.
Save skwp/130d6b18ee90c1c93799 to your computer and use it in GitHub Desktop.
class Admin::Api::BaseController < Admin::BaseController
after_filter :set_csrf_cookie
# For client side js applications needing to qualify for Rails CSRF protection
# Normally rails will return its csrf token in the body of the request for html rendering
#
# In the case of client side apps, we return the token in a cookie
# See: https://technpol.wordpress.com/2014/04/17/rails4-angularjs-csrf-and-devise/
def set_csrf_cookie
cookies['X-CSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
end
private
# Allow client side apps to set CSRF tokens in headers
def verified_request?
super || form_authenticity_token == request.headers['X-CSRF-TOKEN']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment