// Step 1: Get CSRF cookie
await fetch('https://your-domain.com/sanctum/csrf-cookie', {
credentials: 'include'
});
// Step 2: Get CSRF token from cookie
const csrfToken = document.cookie
.split('; ')
.find(row => row.startsWith('XSRF-TOKEN='))
?.split('=')[1];
// Step 3: Login
const response = await fetch('https://your-domain.com/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-XSRF-TOKEN': decodeURIComponent(csrfToken)
},
credentials: 'include',
body: JSON.stringify({
email: '[email protected]',
password: 'password123',
remember: true
})
});
const data = await response.json();
if (data.two_factor) {
// Redirect to 2FA challenge page
console.log('Two-factor authentication required');
} else {
// User is authenticated
console.log('Login successful');
}