30 lines
907 B
TypeScript
30 lines
907 B
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import axios from 'axios';
|
|
import {wrapper} from 'axios-cookiejar-support';
|
|
import {CookieJar} from 'tough-cookie';
|
|
|
|
const jar = new CookieJar();
|
|
const baseClient = wrapper(axios.create({
|
|
headers: {'X-Requested-With': 'XMLHttpRequest'},
|
|
jar,
|
|
}));
|
|
|
|
// Add request interceptor to handle CSRF tokens
|
|
baseClient.interceptors.request.use(async (config) => {
|
|
// Extract CSRF token from MMCSRF cookie and add as header
|
|
const cookies = jar.getCookiesSync(config.url || '');
|
|
const csrfCookie = cookies.find((cookie) => cookie.key === 'MMCSRF');
|
|
|
|
if (csrfCookie && csrfCookie.value) {
|
|
config.headers = config.headers || {};
|
|
config.headers['X-CSRF-Token'] = csrfCookie.value;
|
|
}
|
|
|
|
return config;
|
|
});
|
|
|
|
export const client = baseClient;
|
|
|
|
export default client;
|