Laravel 5.4 EloquentUserProvider override validateCredentials

public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

      

How to override a method validateCredentials

in a class EloquentUserProvider

? Thank!

+4


source to share


2 answers


You can create your own UserProvider and then you can override functions from the original UserProvider.

First, you create a CustomUserProvider:

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class CustomUserProvider extends UserProvider {

  public function validateCredentials(UserContract $user, array $credentials)
  {
    $plain = $credentials['password'];

    return $this->hasher->check($plain, $user->getAuthPassword());
  }

}

      



Then you register your new CustomUserProvider in config / app.php

'providers' => array(
   ... On the bottom, must be down to override the default UserProvider
   'Your\Namespace\CustomUserProvider'
),

      

+2


source


In Laravel 5.4, you don't need to register your CustomUserProvider in config / app.php.

First, create a CustomUserProvider.php file in the Providers directory:

<?php

namespace App\Providers;

use Illuminate\Auth\EloquentUserProvider as UserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;


class CustomUserProvider extends UserProvider {

    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

}

      

After that, change the method boot()

in the AuthServiceProvider.php file:



public function boot()
{
    $this->registerPolicies();

    \Illuminate\Support\Facades\Auth::provider('customuserprovider', function($app, array $config) {
        return new CustomUserProvider($app['hash'], $config['model']);
    });
}

      

You can now use the provider by adding the driver name to your config / auth.php file:

'providers' => [
    'users' => [
        'driver' => 'customuserprovider',
        'model' => App\User::class,
        'table' => 'users',
    ],
],

      

+14


source







All Articles