How do I check if user authenticated? Google result is not helpful since 90% of the answers is to use auth middleware. Lol, I want to perform some query on homepage if user authenticated.
Hmm, sounds like something that might be in the docs?
To determine if the user is already logged into your application, you may use the check method on the Auth facade, which will return true if the user is authenticated:
use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
// The user is logged in...
}
Oh, I just found out the main problem is it's an AJAX. On normal controller that pass through web.php can detect the Auth::check() but the same controller with api.php return false upon calling Auth::check(). What am I suppose to do in this scenario? My frontend is using Vue component and I'm calling axios in vuex.
@jlrdw I'm not doing authentication. I just learnt that just call the ajax in the web.php instead of api.php and have the if(request()->ajax()) to check if it ajax request. Now a GET controller can serve as both ajax and normal http request and Auth::check() work now because now it's session instead of request from api.php.