Skip to content

Commit 96f4474

Browse files
committed
Extract csrfToken and csrfParam to allow external use
1 parent 519aad9 commit 96f4474

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/rails.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,25 @@
5454
// Button onClick disable selector with possible reenable after remote submission
5555
buttonDisableSelector: 'button[data-remote][data-disable-with], button[data-remote][data-disable]',
5656

57+
// Up-to-date Cross-Site Request Forgery token
58+
csrfToken: function() {
59+
return $('meta[name=csrf-token]').attr('content');
60+
},
61+
62+
// URL param that must contain the CSRF token
63+
csrfParam: function() {
64+
return $('meta[name=csrf-param]').attr('content');
65+
},
66+
5767
// Make sure that every Ajax request sends the CSRF token
5868
CSRFProtection: function(xhr) {
59-
var token = $('meta[name="csrf-token"]').attr('content');
69+
var token = rails.csrfToken();
6070
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
6171
},
6272

6373
// making sure that all forms have actual up-to-date token(cached forms contain old one)
6474
refreshCSRFTokens: function(){
65-
var csrfToken = $('meta[name=csrf-token]').attr('content');
66-
var csrfParam = $('meta[name=csrf-param]').attr('content');
67-
$('form input[name="' + csrfParam + '"]').val(csrfToken);
75+
$('form input[name="' + rails.csrfParam() + '"]').val(rails.csrfToken());
6876
},
6977

7078
// Triggers an event on an element and returns false if the event result is false

0 commit comments

Comments
 (0)