Laravel覆盖EloquentUserProvider以更改validateCredentials()中的密码字段名称 [英] Laravel overriding EloquentUserProvider to change password field name in validateCredentials()

查看:725
本文介绍了Laravel覆盖EloquentUserProvider以更改validateCredentials()中的密码字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过覆盖各种类/方法来更改代码中的密码字段.但是,在尝试覆盖EloquentUserProvider及其validateCredentials()方法后,我一直收到错误-

I've managed to change the password field in the code through overriding various classes/methods. But I after trying to override EloquentUserProvider and it's validateCredentials() method I keep getting an error -

ErrorException in AuthUserProvider.php line 15:
Argument 1 passed to App\Providers\AuthUserProvider::__construct() must be an instance of App\Helpers\Sha1Hasher, instance of Illuminate\Foundation\Application given

我创建了一个覆盖App \ Providers \ AuthUserProvider.php-

I created an override App\Providers\AuthUserProvider.php -

namespace App\Providers;

use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use App\Helpers\Sha1Hasher as HasherContract;
use Illuminate\Auth\EloquentUserProvider;

class AuthUserProvider extends EloquentUserProvider
{
/**
 * AuthUserProvider constructor.
 * @param HasherContract $hasher
 * @param string $model
 */
public function __construct(HasherContract $hasher, $model)
{
    parent::__construct($hasher, $model);
}

/**
 * Validate a user against the given credentials.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  array  $credentials
 * @return bool
 */
public function validateCredentials(UserContract $user, array $credentials)
{
    // $plain = $credentials['sha_pass_hash'];
    // return HasherContract::check(...);;
}
}

使用Laravel 5.2.22.

using Laravel 5.2.22.

推荐答案

您如何覆盖" EloquentUserProvider的实例?因为Laravel根据您设置的auth.driver创建Auth实例.

How did you 'override' the instance of EloquentUserProvider ? Because Laravel is creating the Auth instance based on what auth.driver you have set.

检查Illuminate/Auth/CreatesUserProviders@createUserProvider是否已基于驱动程序进行硬编码以加载EloquentUserProvider.您可以尝试的是将实例bind设置为Illuminate\Auth\EloquentUserProvider.

Check Illuminate/Auth/CreatesUserProviders@createUserProvider it is hardcoded, based on driver, to load EloquentUserProvider. What you can try is bind your instance to the Illuminate\Auth\EloquentUserProvider.

您得到的错误意味着您的__construct没有得到正确的输入.根据您的代码的样子,其基本操作如下:

The error you get, means that your __construct isn't getting the proper input. Based on how your code looks like, its basicly doing this:

new AuthUserProvider($app);

但是它应该做什么:

return new EloquentUserProvider($this->app['hash'], $config['model']);

如果它不起作用,请尝试通过AuthManager类注册自定义提供程序.参见Illuminate\Auth\AuthManager@provider第+-266行.

If it is not working try registering a custom provider through the AuthManager class. See Illuminate\Auth\AuthManager@provider line +- 266.

也许!未经测试:

auth()->provider(AuthUserProvider::class);

这篇关于Laravel覆盖EloquentUserProvider以更改validateCredentials()中的密码字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆