【发布时间】:2016-06-10 06:08:45
【问题描述】:
我正在尝试设置自定义身份验证防护,并且一切正常。我可以登录Model,但是一旦我将访问者重定向到新页面,身份验证就会丢失。我可以在控制器执行重定向之前 dd() Auth::guard('client')->user(),但在 AuthenticateClient 中间件中以 null 的形式出现。
我正在使用默认保护来验证用户身份,并且一切正常。我已确保路由位于启用会话的 web 中间件之下。
我已经搜索过类似的问题,但找不到有效的解决方案。任何想法如何解决这个问题?
旁注:我知道我在下面的代码示例中使用了token,但我所做的不仅仅是针对该令牌进行验证。所以这是一个不同于为 api 验证令牌的系统。
路线:
Route::group(['middleware' => 'web'], function () {
// other routes...
Route::get('client/login/{token}', ['as' => 'client.token', 'uses' => 'Auth\ClientController@attemptTokenLogin']);
Route::group(['prefix' => 'client', 'middleware' => 'auth.client'], function () {
Route::get('dashboard', ['as' => 'client.dashboard', 'uses' => 'ClientController@dashboard']);
});
});
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。 -
按预期工作。守卫被命名为
client,但提供者被命名为clients,守卫指向正确的。但是,即使将提供程序更改为client,它仍然不起作用。
标签: php laravel authentication laravel-5