Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 EloquentUserProvider override validateCredentials

Tags:

laravel

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

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

How to override a method validateCredentials in the class EloquentUserProvider?? Thanks!

like image 950
Sergey Somok Avatar asked May 19 '17 11:05

Sergey Somok


2 Answers

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

See this blog article for detailed instructions.

The short form:

First, create a CustomUserProvider.php file in your 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 this, change the boot() Method in your 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']);
    });
}

Now, you can use the provider by adding the driver name to your config/auth.php file:

'providers' => [
    'users' => [
        'driver' => 'customuserprovider',
        'model' => App\User::class,
        'table' => 'users',
    ],
],
like image 71
mhellmeier Avatar answered Oct 23 '22 15:10

mhellmeier


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

First you create the 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'
),
like image 23
Troyer Avatar answered Oct 23 '22 15:10

Troyer