【发布时间】:2019-02-15 10:04:21
【问题描述】:
我正在从 Laravel 4 迁移到 5.7,但我的自定义身份验证提供程序遇到了问题。我遵循了各种演练(例如1、2、3)以及相当多的谷歌搜索。
我已尝试通过以下方式使其正常工作:
设置守卫和提供者并链接到我的目标模型。
'defaults' => [
'guard' => 'custom_auth_guard',
'passwords' => 'users',
],
'guards' => [
'custom_auth_guard' => [
'driver' => 'session',
'provider' => 'custom_auth_provider',
],
],
'providers' => [
'custom_auth_provider' => [
'driver' => 'custom',
'model' => App\UserAccount::class,
],
],
注册上述提供程序中定义的驱动程序。为方便起见,我正在搭载 AuthServiceProvider
...
public function boot()
{
$this->registerPolicies();
\Auth::provider('custom',function() {
return new App\Auth\CustomUserProvider;
});
}
...
创建了我的自定义提供程序,其中包含我的retrieveByCredentials 等。我已将逻辑替换为一些 die() 以验证它是否在此处生成。在 Laravel 4 中,它曾经转到 validateCredentials()。
class CustomUserProvider implements UserProviderInterface {
public function __construct()
{
die('__construct');
}
public function retrieveByID($identifier)
{
die('retrieveByID');
}
public function retrieveByCredentials(array $credentials)
{
die('retrieveByCredentials');
}
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
die('validateCredentials');
}
作为参考,App/UserAccount 看起来像这样
class UserAccount extends Authenticatable
{
use Notifiable;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'public.user_account';
// no updated_at, created_at
public $timestamps = false;
private $_roles = [];
private $_permissions = [];
}
最后,我通过我的控制器调用它。
if(\Auth::attempt($credentials){
return \Redirect::intended('/dashboard');
}
我也试过直接打电话给守卫
if(\Auth::guard('custom_auth_guard')->attempt($credentials){
return \Redirect::intended('/dashboard');
}
这会导致以下错误:"Auth guard [custom_auth_guard] is not defined."
我已经尝试了一些其他命令来确保没有缓存问题:
composer update
php artisan cache:clear
结果:当我调用 Auth::attempt($credentials) Laravel 试图在 users 表上运行查询。预期的结果是它会命中 CustomUserProvider 中的 die() 之一……或者至少尝试查询模型中定义的 public.user_account。
我已经搞砸了一段时间,我一定错过了一些简单的东西......希望对 Laravel 5 有更多经验的人可以看到我做错了什么。
提前致谢!!
【问题讨论】:
标签: laravel