Reputation: 1222
I had an issue with Laravel when it throws a 404 exception, or any kind of exception, the auth() helper returns null in these pages, 404, 400... any exception.
As someone suggested, I moved 2 middlewares to the globalgroup
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
however, it caused another issue, it was logging me out everytime i refresh the page, when I moved these middlewares back logged in users stay logged in.
So now, I still need a way to use auth(), in 404 pages, it makes no sense that the navbar displays Please Login on 404 pages when the use is already logged in, that's really bad Laravel.
Actually the solution above suggested by many, cause lots of issues in the code, and detecting the authenticated user, I just did some tests, it was creating a new session upon every refresh.
Upvotes: 0
Views: 2524
Reputation: 1222
Issue is solved at my previous related question auth() method null on Exceptions
These middlewares should be moved and not copied to the global array. I had multiple instances, on web group and global group, which was causing the issue.
So copying the middlwares in App/Http/Kernel.php
from the 'web' group to the 'middlewares' group will recreate the session on every refresh. I hope, someone will find this useful.
Upvotes: 1