我想設置一個自定義身份驗證守衛,一切都主要工作。我可以登錄Model
,但是一旦我將訪問者重定向到新頁面,身份驗證就會丟失。我可以dd()Auth::guard('client')->user()
在控制器執行重定向之前就好了,但在AuthenticateClient
中間件中出現爲null
。Laravel [5.2.21]自定義身份驗證守衛不堅持
我正在使用默認警衛來認證用戶,一切都正常工作。我已經確定路線是在啓用會話的web
中間件下。
我已經搜索了類似的問題,但我找不到可行的解決方案。任何想法如何解決這一問題?
附註:我知道我在下面的代碼示例中使用了token
,但我所做的不僅僅是針對該令牌進行驗證。所以這是一個與驗證api令牌不同的系統。
途徑:
Route::group(['middleware' => 'web'], function() {
// other routes...
Route::get('client/login/{token}', ['as' => 'client.token', 'uses' => 'Auth\[email protected]']);
Route::group(['prefix' => 'client', 'middleware' => 'auth.client'], function() {
Route::get('dashboard', ['as' => 'client.dashboard', 'uses' => '[email protected]']);
});
});
auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
// new auth guard
'client' => [
'driver' => 'session',
'provider' => 'clients',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// new guard provider
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class,
],
],
];
HTTP/Kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
// new...
'auth.client' => \App\Http\Middleware\AuthenticateClient::class,
];
ClientController @ attemptTokenLogin
$client = // get the client in a custom way...
Auth::guard('client')->login($client);
// dd(Auth::guard('client')->user()); // this works here
return redirect()->route('client.dashboard');
AuthenticateClient
public function handle($request, Closure $next)
{
// dd(Auth::guard('client')->user()); // this does not work here
if (!Auth::guard('client')->check()) {
return redirect()->route('client.login');
}
return $next($request);
}
在'providers'數組你使用'clients'但後衛是'client'。 –
按預期工作。守衛被命名爲「客戶」,但提供者被命名爲「客戶」,守衛指向正確的一位。但是,即使將提供者更改爲「客戶端」,它仍然不起作用。 –