Files
SAE-BUT2-frontend/src/utils/getXSRF.js
T

20 lines
594 B
JavaScript

export default async function getXSRFToken() {
const response = await fetch(`http://${import.meta.env.VITE_API_URL}/sanctum/csrf-cookie`, {
method: 'GET',
credentials: 'include'
});
if (!response.ok && response.status !== 204) {
throw new Error(`Error : ${response.status}`);
}
const name = 'XSRF-TOKEN';
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) {
return decodeURIComponent(parts.pop().split(';').shift());
}
throw new Error('Error: Invalid XSRF-TOKEN');
}