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!
My Answer:
It looks like the issue might be with the way you are defining the guards in your auth.php file. Instead of using 'guard' => 'web' in the defaults, try using 'guard' => 'user' since you have defined a 'user' guard in your guards section.
Also, make sure that you are using the correct guard when checking for the authenticated user in your middleware. For example, in RedirectIfAuthenticated.php, you should use Auth::guard($guard)->check() instead of just Auth::check().
Additionally, make sure that you have set the guard correctly in your User and Admin models. The correct property name should be 'guard' instead of 'protected $guard'.
After making these changes, try dumping Auth::user() again on the index page to see if it returns the authenticated user data correctly.
Rate this post
4 of 5 based on 5023 votesComments