Auth::user() returns null
Any route that uses Auth()
must be encapsulated in the web
middleware. You're close, just move your Route::group(['prefix' => 'admin'], ...)
into the group above.
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'HomeController@index');
// Moving here will ensure that sessions, csrf, etc. is included in all these routes
Route::group(['prefix'=>'admin', 'middleware' => 'admin'], function(){
Route::get('/', function(){
return view('admin.index');
});
Route::get('/user', function(){
return view('admin.user');
});
});
});
Auth::user() returns null in Laravel 6
So, I don't know why that happens, and after finally giving up, I came up with a (not so clean) solution.
Now, instead of using multiple guards, I'm creating a relationship from the users table to the guru/admin table.
This is how I access it now Auth::user()->guru
or Auth::user()->admin
I know this is not a good solution, but it's the fastest workaround I can found
Auth::user() returns null in Laravel 5.2
Thank you guys.
The problem solved here:
https://laracasts.com/discuss/channels/laravel/authuser-returns-null-in-laravel-52
I had to add the stack into middleware directly (not in the group).
in /Http/kernel.php:
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class
];
Laravel Auth::user() always null
I have fixed this error. I have created an authentication route in route/api.php. API route not supported to the session so it does not store user data in the session because that Auth::user() always returns null. please view this answer https://stackoverflow.com/a/44863089/13729121
Related Topics
Laravel 5 - Env() Always Returns Null
Getting the Value of an Extra Pivot Table Column Laravel
How to Properly Use Bearer Tokens
Parallel Processing in PHP - How to Do It
Is_Null($X) VS $X === Null in PHP
Laravel Eloquent Update Just If Changes Have Been Made
Handling Put/Delete Arguments in PHP
How to Inject a Repository into a Service in Symfony
Best Way to Store Passwords in MySQL Database
Best Way to Handle Errors on a PHP Page
How to Display "12 Minutes Ago" etc in a PHP Webpage
Login Only If User Is Active Using Laravel
After Array_Filter(), How to Reset the Keys to Go in Numerical Order Starting at 0