【问题标题】:Laravel [5.2.21] Custom Auth Guard not persistingLaravel [5.2.21] 自定义 Auth Guard 不持久
【发布时间】: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


【解决方案1】:

在实施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();
}

【讨论】:

  • 连续 15 小时试图弄清楚如何解决这个问题......有趣的是,这个问题是我第一次访问但被它的简单性忽略了...... 15 小时后,在我弄清楚了自己研究了整个框架及其请求周期后,想起了这个问题……如果我一开始尝试这个,我会节省15个小时的无用头痛。
  • 顺便说一句,您不需要在路由中添加 web 中间件。从 5.2 开始,这是默认行为
  • 3 天。这就是这个问题折磨我的时间。
  • 我正在开发 Laravel 5.4,对我来说同样的问题仍然存在,请您指导我在哪里修改上述代码。
  • @ReiahPaulSam 这是我的新 App\Client 模型类。
猜你喜欢
  • 2020-04-22
  • 2016-03-11
  • 1970-01-01
  • 2015-12-21
  • 2016-05-07
  • 2016-09-07
  • 2021-10-28
  • 2017-07-29
  • 2017-09-24
相关资源
最近更新 更多