Skip to content

Commit

Permalink
Set X-CSRF-Token on HTTP requests
Browse files Browse the repository at this point in the history
https://github.com/emberjs/ember-rails#csrf-token
https://github.com/thoughtbot/ember-cli-rails#csrf-tokens

When `ember-cli-rails` embeds the CSRF meta tags, this initializer
configures the host app to recognize them and set them as outgoing HTTP
headers to play nice with the Rails app's CSRF protection.
  • Loading branch information
seanpdoyle committed Nov 10, 2015
1 parent 2f2a47e commit 32a0d16
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/initializers/csrf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Ember from 'ember';

const { $ } = Ember;

export default {
name: 'ember-cli-rails-addon:csrf',

initialize() {
$.ajaxPrefilter((options, originalOptions, xhr) => {
const token = $('meta[name="csrf-token"]').attr('content');
xhr.setRequestHeader('X-CSRF-Token', token);
});
},
};

0 comments on commit 32a0d16

Please sign in to comment.