Home > front end >  Laravel 8 auth returns null
Laravel 8 auth returns null

Time:10-01

Problem:

  • On my index page, authenticated users and guests should be able to view and use the page unless they want to save something. I tried to log in so that I could save the data and was redirected to an authenticated page which is a profile page. I tried dumping Auth::user() on the profile page, it returns the user logged in data, tried dumping it again on the index page then it returns null.

I'm using the latest version of Laravel.

I've googled some of the same problems I have and the closest one is this Auth::user() returns null. I tried following the answer provided but still, it returns null.

web.php

Route::group(['middleware' => 'web'], function () {
    Route::get('/', [IndexController::class, 'index'])->name('index.page');

    Auth::routes();
    
    Route::group(['middleware' => ['auth:user']], function() {
        Route::get('/user/profile', [UserController::class, 'profile'])->name('user.profile');
    });

    Route::group(['prefix' => 'admin', 'middleware' => ['auth:admin']], function() {
        Route::get('dashboard',[DashboardController::class, 'index'])->name('admin.dashboard');

    });
})

auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'user' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Models\Admin::class,
    ],
],
'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
        'throttle' => 60,
    ],
    'admins' => [
        'provider' => 'admins',
        'table' => 'password_resets',
        'expire' => 15,
   ],
],

This is the only modification I did from the middleware.

RedirectIfAuthenticated.php

foreach ($guards as $guard) {
   if ($guard === "admin" && Auth::guard($guard)->check()) {
       return redirect('/admin/dashboard');
   }

   if ($guard === "user" && Auth::guard($guard)->check()) {
       return redirect('/user/profile');
   }

   if (Auth::guard($guard)->check()) {
       return redirect('/');
   }
}

I added $guard for the User and Admin model

User.php

protected $guard = 'user';

Admin.php

protected $guard = 'admin';

If you have any questions, feel free to ask and thanks for the help!

CodePudding user response:

I can share the solution that worked for me. I switched from Passport to Sanctum.

CodePudding user response:

But i guess the customized Auth.php has a problem

 foreach ($guards as $guard) {
   if ($guard === "admin" && Auth::guard($guard)->check()) {
       return redirect('/admin/dashboard');
   }else{
       if ($guard === "user" && Auth::guard($guard)->check()) {
          return redirect('/user/profile');
       }else{
          return redirect('/');
       }   
   }

}
  • Related