I created a new global middleware to be used every time a request to a new page is received, and I'm using Auth::check() inside it but even while logged in, it always returns false. I've tried alot of things I read on other questions regarding the same subject but nothing worked. Here is the code:
Kernel.php
protected $middleware = [
...
AppHttpMiddlewareUserSessionDuration::class,
...
];
UserSessionDuration.php
class UserSessionDuration
{
public function handle(Request $request, Closure $next)
{
Log::emergency('we got hereeee');
// Check if the user is logged in
if (!Auth::check()) {
Log::emergency('auth check failed');
return $next($request);
}
else { do something else }
}
}
The way I know that it always enters that !Auth::check() clause is because every time I load a new page, it prints to the laravel.log file the message.
I've tried:
writing $request->user() instead of Auth::check()
use Auth and use IlluminateSupportFacadesAuth
running php artisan cache:clear, composer dump-autoload, php artisan clear-compiled, php artisan optimize
If anyone knows how I could fix this, please let me know as it's been haunting me for some time now...
Thanks alot for your time :)