(asFormFallback = false)
| 165 | } |
| 166 | |
| 167 | async function retryWithFreshCsrf(asFormFallback = false) { |
| 168 | const tokRes = await original('/api/auth/token.php', { credentials: 'include' }); |
| 169 | if (tokRes.ok) { |
| 170 | const body = await tokRes.json().catch(() => ({})); |
| 171 | if (body?.csrf_token) { |
| 172 | window.csrfToken = body.csrf_token; |
| 173 | const meta = document.querySelector('meta[name="csrf-token"]'); |
| 174 | if (meta) meta.content = body.csrf_token; |
| 175 | options.headers['X-CSRF-Token'] = body.csrf_token; |
| 176 | } |
| 177 | } |
| 178 | if (asFormFallback && wantJson) { |
| 179 | // convert JSON body into x-www-form-urlencoded |
| 180 | const orig = options.body && typeof options.body === 'string' ? JSON.parse(options.body) : {}; |
| 181 | options.body = toFormBody(orig); |
| 182 | options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 183 | } |
| 184 | return original(url, options); |
| 185 | } |
| 186 | |
| 187 | let res = await original(url, options); |
| 188 |
no test coverage detected