2016-02-26 49 views
3

我想設置一個自定義身份驗證守衛,一切都主要工作。我可以登錄Model,但是一旦我將訪問者重定向到新頁面,身份驗證就會丟失。我可以dd()Auth::guard('client')->user()在控制器執行重定向之前就好了,但在AuthenticateClient中間件中出現爲nullLaravel [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); 
} 
+0

在'providers'數組你使用'clients'但後衛是'client'。 –

+0

按預期工作。守衛被命名爲「客戶」,但提供者被命名爲「客戶」,守衛指向正確的一位。但是,即使將提供者更改爲「客戶端」,它仍然不起作用。 –

回答

3

在實施Illuminate\Contracts\Auth\Authenticatable我不回來getAuthIdentifierName()getAuthIdentifier()

所以......

public function getAuthIdentifierName() 
{ 
    $this->getKeyName(); 
} 

public function getAuthIdentifier() 
{ 
    $this->getKey(); 
} 

應該是...

public function getAuthIdentifierName() 
{ 
    return $this->getKeyName(); 
} 

public function getAuthIdentifier() 
{ 
    return $this->getKey(); 
} 
+1

連續15個小時試圖找出如何解決這個問題...有趣的是,這個問題是我第一次訪問,但忽略了它的簡單性...... 15小時後,在我研究了整個框架和它的請求週期,我想起了這個問題......如果我一開始就試過這個,我會節省15個小時的無用頭痛。 – Frondor

+0

順便說一句,你不需要添加'網絡'中間件到你的路線。由於5.2這是默認行爲 – Frondor

+0

3天。這個問題折磨了我多久。 –