4

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.

17
  • Please you be more specific about what you wanna achieve here. Did you change password column name in users table ? Commented Mar 1, 2016 at 16:59
  • yes i've changed the password column name to sha1_pass and have a custom Hasher. What I'm trying to do is override the validateCredentials() method in the EloquentUserProvider class since it has the password column name hardcoded as $credentials['password']; and also apply a check with the Sha1 Hasher. Commented Mar 2, 2016 at 7:58
  • Password column is not hardcoded in Laravel auth system. You can tell the Auth system to use a specified column as password column Commented Mar 2, 2016 at 8:54
  • I've done that within the Users.php model where you can override getAuthPassword method of the Authenticatable class and change $this->password variable to the name of your column. Now unless i'm missing something, there is a final call to validateCredentials() in EloquentUserProvider/DatabaseUserProvider class before logging you in where it has the 'password' field hardcoded and verifies the hashed password with the plain text. Commented Mar 2, 2016 at 9:43
  • All I am saying why go through this stress when you can do this: if(Auth::attempt(['new_password_column' => $password, 'new_username_column'=>$username])) { //login success } Commented Mar 2, 2016 at 10:16

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This helped a lot!
But did it answer your question?
Well, sort of, it made me dig a little deeper and learn more as i figure it out, so thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.