Skip to content

Commit 469967d

Browse files
committed
Send XSRF-TOKEN cookie as x-csrf-token header in api() and adminApi() for mutating requests
1 parent 77628e9 commit 469967d

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

public/js/admin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ async function adminApi(path, options = {}) {
158158
if (adminState.token) {
159159
headers['Authorization'] = `Bearer ${adminState.token}`;
160160
}
161+
const method = (options.method || 'GET').toUpperCase();
162+
if (method !== 'GET' && method !== 'HEAD') {
163+
const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]*)/);
164+
if (match) headers['x-csrf-token'] = match[1];
165+
}
161166
let res;
162167
try {
163168
res = await fetch(`/api/admin${path}`, { ...options, headers });

public/js/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ async function api(path, options = {}) {
258258
if (state.token) {
259259
headers['Authorization'] = `Bearer ${state.token}`;
260260
}
261+
const method = (options.method || 'GET').toUpperCase();
262+
if (method !== 'GET' && method !== 'HEAD') {
263+
const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]*)/);
264+
if (match) headers['x-csrf-token'] = match[1];
265+
}
261266
let res;
262267
try {
263268
res = await fetch(`${API_BASE}/api${path}`, { ...options, headers });

0 commit comments

Comments
 (0)