SagunKho
SagunKho

Reputation: 1059

Laravel overriding EloquentUserProvider to change password field name in 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

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.

Upvotes: 4

Views: 2968

Answers (1)

Yoram de Langen
Yoram de Langen

Reputation: 5499

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

Upvotes: 2

Related Questions