|
| 1 | +/* global $ */ |
1 | 2 | const orig$ = $; |
2 | 3 |
|
3 | 4 | function fetchAntiForgeryToken() { |
4 | | - const d = orig$.Deferred(); |
5 | | - |
6 | | - orig$.ajax({ |
7 | | - url: `https://js.devexpress.com/Demos/NetCore/api/Common/GetAntiForgeryToken`, |
8 | | - method: 'GET', |
9 | | - xhrFields: { withCredentials: true }, |
10 | | - cache: false, |
11 | | - }).done((data) => { |
12 | | - d.resolve(data); |
13 | | - }).fail((xhr) => { |
14 | | - const error = xhr.responseJSON?.message || xhr.statusText || 'Unknown error'; |
15 | | - d.reject(new Error(`Failed to retrieve anti-forgery token: ${error}`)); |
16 | | - }); |
17 | | - return d.promise(); |
| 5 | + const d = orig$.Deferred(); |
| 6 | + |
| 7 | + orig$.ajax({ |
| 8 | + url: 'https://js.devexpress.com/Demos/NetCore/api/Common/GetAntiForgeryToken', |
| 9 | + method: 'GET', |
| 10 | + xhrFields: { withCredentials: true }, |
| 11 | + cache: false, |
| 12 | + }).done((data) => { |
| 13 | + d.resolve(data); |
| 14 | + }).fail((xhr) => { |
| 15 | + const error = xhr.responseJSON?.message || xhr.statusText || 'Unknown error'; |
| 16 | + d.reject(new Error(`Failed to retrieve anti-forgery token: ${error}`)); |
| 17 | + }); |
| 18 | + return d.promise(); |
18 | 19 | } |
19 | 20 |
|
20 | 21 | function getAntiForgeryTokenValue() { |
21 | | - const tokenMeta = document.querySelector('meta[name="csrf-token"]'); |
22 | | - if (tokenMeta) { |
23 | | - const headerName = tokenMeta.dataset.headerName || 'RequestVerificationToken'; |
24 | | - const token = tokenMeta.getAttribute('content'); |
25 | | - return orig$.Deferred().resolve({ headerName, token }); |
26 | | - } |
| 22 | + const tokenMeta = document.querySelector('meta[name="csrf-token"]'); |
| 23 | + if (tokenMeta) { |
| 24 | + const headerName = tokenMeta.dataset.headerName || 'RequestVerificationToken'; |
| 25 | + const token = tokenMeta.getAttribute('content'); |
| 26 | + return orig$.Deferred().resolve({ headerName, token }); |
| 27 | + } |
27 | 28 |
|
28 | | - return fetchAntiForgeryToken().then((tokenData) => { |
29 | | - const meta = document.createElement('meta'); |
30 | | - meta.name = 'csrf-token'; |
31 | | - meta.content = tokenData.token; |
32 | | - meta.dataset.headerName = tokenData.headerName; |
33 | | - document.head.appendChild(meta); |
34 | | - return tokenData; |
35 | | - }); |
| 29 | + return fetchAntiForgeryToken().then((tokenData) => { |
| 30 | + const meta = document.createElement('meta'); |
| 31 | + meta.name = 'csrf-token'; |
| 32 | + meta.content = tokenData.token; |
| 33 | + meta.dataset.headerName = tokenData.headerName; |
| 34 | + document.head.appendChild(meta); |
| 35 | + return tokenData; |
| 36 | + }); |
36 | 37 | } |
37 | 38 |
|
38 | 39 | async function setAntiForgery() { |
39 | | - const originalAjax = orig$.ajax; |
40 | | - const tokenData = await getAntiForgeryTokenValue(); |
41 | | - |
42 | | - $ = orig$; |
43 | | - |
44 | | - $.ajax = (url, options) => { |
45 | | - if (typeof url !== 'string') { |
46 | | - options = url; |
47 | | - } else { |
48 | | - options.url = url; |
49 | | - } |
50 | | - options.headers = { [tokenData.headerName]: tokenData.token, ...(options.headers || {}) }; |
51 | | - options.xhrFields = {withCredentials: true, ...(options.xhrFields || {})}; |
52 | | - |
53 | | - return originalAjax.call(this, options); |
54 | | - }; |
55 | | -} |
| 40 | + const originalAjax = orig$.ajax; |
| 41 | + const tokenData = await getAntiForgeryTokenValue(); |
| 42 | + |
| 43 | + // eslint-disable-next-line no-global-assign |
| 44 | + $ = orig$; |
56 | 45 |
|
| 46 | + $.ajax = (url, options) => { |
| 47 | + if (typeof url !== 'string') { |
| 48 | + // eslint-disable-next-line no-param-reassign |
| 49 | + options = url; |
| 50 | + } else { |
| 51 | + options.url = url; |
| 52 | + } |
| 53 | + options.headers = { [tokenData.headerName]: tokenData.token, ...(options.headers || {}) }; |
| 54 | + options.xhrFields = { withCredentials: true, ...(options.xhrFields || {}) }; |
| 55 | + |
| 56 | + return originalAjax.call(this, options); |
| 57 | + }; |
| 58 | +} |
57 | 59 |
|
| 60 | +// eslint-disable-next-line no-global-assign |
58 | 61 | $ = (...args) => orig$(async () => { |
59 | | - await setAntiForgery(); |
60 | | - |
61 | | - return $(...args); |
62 | | -}) |
| 62 | + await setAntiForgery(); |
| 63 | + |
| 64 | + return $(...args); |
| 65 | +}); |
0 commit comments