Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GTHell's avatar

Check if user authenticated?

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.

0 likes
9 replies
jlrdw's avatar

Laravel has documentation?

3 likes
ndotie's avatar

@jlrdw Sure... but quick response wont hurt.... its helpful when on midst of deadline

tykus's avatar

And you have really nailed that quick response @ndotie - why resurrect a 4 year old thread with such an inane comment???

GTHell's avatar

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's avatar

That reminds me of Paul Harvey I used to listen to him on the radio.

now the rest of the story

I don't use an API Cronix will probably answer but you need token-based Authentication.

Many tutorials available.

Danlog's avatar

If you want to check it on the view, you do @if(Auth::check())

now let's get deeper...

@if(Auth::check())
//HERE IS WHAT WILL APPEAR IF THE USER IS LOGGED IN
@else 
//HERE IS WHAT WILL APPEAR IF THE USER IS NOT LOGGED IN
@endif 

as easy as that.

GTHell's avatar

@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.

Thank for all the answers though.

Please or to participate in this conversation.