Laravel overriding EloquentUserProvider to change password field name in validateCredentials()

筅森魡賤 提交于 2019-12-10 06:46:44

问题


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

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(...);;
}
}

using Laravel 5.2.22.


回答1:


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

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.

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);

But what it should do:

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

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

Maybe!! not tested:

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


来源:https://stackoverflow.com/questions/35728219/laravel-overriding-eloquentuserprovider-to-change-password-field-name-in-validat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!