// Step 1: Get CSRF cookie
await fetch('https://your-domain.com/sanctum/csrf-cookie', {
credentials: 'include'
});
// Step 2: Get CSRF token
const csrfToken = document.cookie
.split('; ')
.find(row => row.startsWith('XSRF-TOKEN='))
?.split('=')[1];
// Step 3: Register
const response = await fetch('https://your-domain.com/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-XSRF-TOKEN': decodeURIComponent(csrfToken)
},
credentials: 'include',
body: JSON.stringify({
name: 'John Doe',
email: '[email protected]',
password: 'SecurePass123!',
password_confirmation: 'SecurePass123!'
})
});
if (response.ok) {
const user = await response.json();
console.log('Registration successful:', user);
// User is now authenticated
} else {
const errors = await response.json();
console.error('Registration failed:', errors);
}