Unable to access auth guard user for custom authenticated user

huangapple go评论55阅读模式
英文:

Unable to access auth guard user for custom authenticated user

问题

I have created an authentication guard for staff in config/auth:

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

    'staff' => [
        'driver' => 'session',
        'provider' => 'staff',
    ],
],

I have tried to access properties of logged in staff using:

{{ Auth::guard('staff')->user()->surname }}

but I get an error:

"Attempt to read property 'surname' on null."

web.php:

Route::middleware('staff')->group(function () {

    Route::get('staffdashboard', function () {
        return view('staff.staffdashboard');
    })->name('staffdashboard');

});
英文:

I have created an authentication guard for staff in config/auth

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

        'staff' => [
            'driver' => 'session',
            'provider' => 'staff',
        ],
    ],

I have tried to access properties of logged in staff using

{{ Auth::guard('staff')->user()->surname}}

but i get error

Attempt to read property "surname" on null.

web.php

Route::middleware('staff')->group(function () {

Route::get('staffdashboard', function () {
    return view('staff.staffdashboard');
})->name('staffdashboard');

});

答案1

得分: 1

将中间件名称从staff更改为auth:staff,而不是web.php文件中的staff

Route::middleware('auth:staff')->group(function () {

  Route::get('staffdashboard', function () {
     return view('staff.staffdashboard');
  })->name('staffdashboard');

});
英文:

Just change the middleware name to auth:staff instead of staff in your web.php file

Route::middleware('auth:staff')->group(function () {

  Route::get('staffdashboard', function () {
     return view('staff.staffdashboard');
  })->name('staffdashboard');

});

huangapple
  • 本文由 发表于 2023年5月8日 02:11:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定