Problem with authentification between laravel/react
, when fetch get, all work:
api.php
Route::group(['middleware' => ['web']], function () {
Route::post('login','Auth\LoginController@login');
});
login.js
getCookie(name) {
if (!document.cookie) {
return null;
}
const xsrfCookies = document.cookie.split(';')
.map(c => c.trim())
.filter(c => c.startsWith(name + '='));
if (xsrfCookies.length === 0) {
return null;
}
return decodeURIComponent(xsrfCookies[0].split('=')[1]);
}
const csrfToken = this.getCookie('CSRF-TOKEN');
const headers = new Headers({
'Content-Type': 'application/json',
'X-XSRF-TOKEN': csrfToken
});
fetch('api/login',
{
method: 'POST',
headers,
body: JSON.stringify( this.state )
})
.then(response=> {
console.log(response);
this.setState({err: false});
this.props.history.push('/') ;
})
.catch(error=> {
console.log(error);
this.refs.email.value='';
this.refs.password.value='';
this.setState({err: true});
});