Skip to content

Instantly share code, notes, and snippets.

@kieran
Last active December 18, 2015 16:09
Show Gist options
  • Save kieran/5809865 to your computer and use it in GitHub Desktop.
Save kieran/5809865 to your computer and use it in GitHub Desktop.
class Api::V1::ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_csrf_header
private
def set_csrf_header
response.headers['X-CSRF-Token'] = session[:_csrf_token] ||= SecureRandom.base64(32)
end
end
$.ajaxSetup
xhrFields:
withCredentials: true
$(document).ajaxSend (event, jqxhr, settings)->
jqxhr.setRequestHeader 'X-CSRF-Token', Embed.get 'csrf-token'
$(document).ajaxComplete (event, jqxhr, settings)->
if jqxhr.getResponseHeader('X-CSRF-Token')?
Embed.set 'csrf-token', jqxhr.getResponseHeader 'X-CSRF-Token'
module Api
module V1
class ListingsController < ApplicationController
# ...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment