Skip to content

Commit

Permalink
added CRSF helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dzirtusss committed Aug 11, 2016
1 parent 23c4ba3 commit ad7fb92
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions node_package/src/ReactOnRails.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash';
import * as ClientStartup from './clientStartup';
import handleError from './handleError';
import ComponentRegistry from './ComponentRegistry';
Expand Down Expand Up @@ -72,23 +73,29 @@ ctx.ReactOnRails = {
ClientStartup.reactOnRailsPageLoaded();
},

authenticityHeader (options) {
options = options || {};
return Object.assign(options, {
'X-CSRF-Token': getAuthenticityToken(),
'X-Requested-With': 'XMLHttpRequest'
});
},

/**
* Return CFRS authenticity token inserted by Rails
*
* @returns CFRS token
*/

getAuthenticityToken () {
let header = document.querySelector(`meta[name="csrf-token"]`);
return header && header.content;
getAuthenticityToken() {
const token = _.find(document.querySelectorAll('meta'), ['name', 'csrf-token']);
return token ? token.content : null;
},

/**
*
* @param options
* @returns {*}
*/

authenticityHeader(options) {
options = options || {};
return Object.assign(options, {
'X-CSRF-Token': ReactOnRails.getAuthenticityToken(),
'X-Requested-With': 'XMLHttpRequest'
});
},

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit ad7fb92

Please sign in to comment.