Files
Pinry/pinry-spa/src/components/utils/csrf.js
2019-12-08 12:58:50 -05:00

38 lines
828 B
JavaScript

import axios from 'axios';
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
return null;
}
function getCSRFToken() {
return getCookie('csrftoken');
}
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
function setUpAxiosCsrfConfig() {
axios.interceptors.request.use(
(config) => {
if (!csrfSafeMethod(config.method.toUpperCase())) {
// eslint-disable-next-line no-param-reassign
config.headers['X-CSRFToken'] = getCSRFToken();
}
return config;
},
(error) => {
Promise.reject(error);
},
);
}
export default setUpAxiosCsrfConfig;