I want to check if user is logged in through JavaScript fetch()
function. But it always returns false, but if I called the URL directly in address bar, it returns true as it is supposed to. Here is the code:
/routes/web.php
Route::get('check-login', function () {
if (Auth::check()) {
return response()->json(['isLogin'=>'ok']);
} else {
return response()->json(['isLogin'=>'no']);
}
});
javascript:
fetch('/check-login', {
headers:{
'Accept': 'application/json'
}
})
.then(response => {
return response.json();
})
.then(result => {
console.log(result);
});
What's wrong with my method?