-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added CSRF helpers authenticityToken and authenticityHeaders
- Loading branch information
Showing
7 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default { | ||
|
||
authenticityToken() { | ||
const token = document.querySelector('meta[name="csrf-token"]'); | ||
return token ? token.content : null; | ||
}, | ||
|
||
authenticityHeaders(otherHeaders = {}) { | ||
return Object.assign(otherHeaders, { | ||
'X-CSRF-Token': this.authenticityToken(), | ||
'X-Requested-With': 'XMLHttpRequest' | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import test from 'tape'; | ||
import ReactOnRails from '../src/ReactOnRails.js'; | ||
|
||
test('authenticityToken and authenticityHeaders', (assert) => { | ||
assert.plan(4); | ||
|
||
assert.ok(typeof ReactOnRails.authenticityToken == 'function', | ||
'authenticityToken function exists in ReactOnRails API'); | ||
|
||
assert.ok(typeof ReactOnRails.authenticityHeaders == 'function', | ||
'authenticityHeaders function exists in ReactOnRails API'); | ||
|
||
const testToken = 'TEST_CSRF_TOKEN'; | ||
|
||
var meta = document.createElement('meta'); | ||
meta.name = 'csrf-token'; | ||
meta.content = testToken; | ||
document.head.appendChild(meta); | ||
|
||
var realToken = ReactOnRails.authenticityToken(); | ||
|
||
assert.equal(realToken, testToken, | ||
'authenticityToken can read Rails CSRF token from <meta>'); | ||
|
||
var realHeader = ReactOnRails.authenticityHeaders(); | ||
|
||
assert.deepEqual(realHeader, { 'X-CSRF-Token': testToken, 'X-Requested-With': 'XMLHttpRequest' }, | ||
'authenticityHeaders returns valid header with CFRS token' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,4 @@ test('setStore and getStore', (assert) => { | |
|
||
assert.deepEqual(ReactOnRails.stores(), expected); | ||
}); | ||
|