Skip to content

Commit ad7fb92

Browse files
committed
added CRSF helpers
1 parent 23c4ba3 commit ad7fb92

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

node_package/src/ReactOnRails.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash';
12
import * as ClientStartup from './clientStartup';
23
import handleError from './handleError';
34
import ComponentRegistry from './ComponentRegistry';
@@ -72,23 +73,29 @@ ctx.ReactOnRails = {
7273
ClientStartup.reactOnRailsPageLoaded();
7374
},
7475

75-
authenticityHeader (options) {
76-
options = options || {};
77-
return Object.assign(options, {
78-
'X-CSRF-Token': getAuthenticityToken(),
79-
'X-Requested-With': 'XMLHttpRequest'
80-
});
81-
},
82-
8376
/**
8477
* Return CFRS authenticity token inserted by Rails
8578
*
8679
* @returns CFRS token
8780
*/
8881

89-
getAuthenticityToken () {
90-
let header = document.querySelector(`meta[name="csrf-token"]`);
91-
return header && header.content;
82+
getAuthenticityToken() {
83+
const token = _.find(document.querySelectorAll('meta'), ['name', 'csrf-token']);
84+
return token ? token.content : null;
85+
},
86+
87+
/**
88+
*
89+
* @param options
90+
* @returns {*}
91+
*/
92+
93+
authenticityHeader(options) {
94+
options = options || {};
95+
return Object.assign(options, {
96+
'X-CSRF-Token': ReactOnRails.getAuthenticityToken(),
97+
'X-Requested-With': 'XMLHttpRequest'
98+
});
9299
},
93100

94101
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)