Don't set X-XSRF-TOKEN when the user isn't logged in

An HTTP header cannot be empty, and although some webservers allow this
(nginx, Apache), others answer 400 Bad Request (lighttpd), preventing
the widget from loading.
This commit is contained in:
Namkhai B
2021-06-15 02:01:57 -05:00
committed by Umputun
parent 1847184960
commit 98bfc7f5f5
2 changed files with 9 additions and 3 deletions
+2 -2
View File
@@ -4,7 +4,7 @@ jest.mock('./settings', () => ({
import { RequestError } from 'utils/errorUtils';
import { API_BASE, BASE_URL } from './constants.config';
import { apiFetcher, authFetcher, adminFetcher, JWT_HEADER, XSRF_HEADER } from './fetcher';
import { apiFetcher, authFetcher, adminFetcher, JWT_HEADER } from './fetcher';
type FetchImplementaitonProps = {
status?: number;
@@ -31,7 +31,7 @@ function mockFetch({ headers = {}, data = {}, ...props }: FetchImplementaitonPro
}
describe('fetcher', () => {
const headers = { [XSRF_HEADER]: '' };
const headers = {};
const apiUri = '/anything';
const apiUrl = `${BASE_URL}${API_BASE}/anything?site=remark`;
+7 -1
View File
@@ -45,7 +45,13 @@ const createFetcher = (baseUrl: string = ''): Methods => {
if (activeJwtToken) {
headers[JWT_HEADER] = activeJwtToken;
}
headers[XSRF_HEADER] = getCookie(XSRF_COOKIE) || '';
// An HTTP header cannot be empty.
// Although some webservers allow this (nginx, Apache), others answer 400 Bad Request (lighttpd).
const xsrfToken = getCookie(XSRF_COOKIE);
if (xsrfToken !== undefined) {
headers[XSRF_HEADER] = xsrfToken;
}
if (body instanceof FormData) {
// Shouldn't add any kind of `Content-Type` if we send `FormData`