Route (web.php)
Route::get('user', function () {
$user=Auth::user();
return response(['user_id'=>$user->id],200);
});
React.js fetch()
fetch('/user')
.then(response => {
return response.json();
})
.then(json => {
console.log(json);
});
When visiting /user
in a browser:
{"user_id":1}
When running the fetch()
method:
Trying to get property of non-object
This is because $user=Auth::user();
is returning null
.
I know this is not a csrf
issue because returning static content (such as user_id=>1
) works fine with the fetch method.
Here are the cookies being sent with the request:
Whats stopping the user session from working? Been messing with this for hours.