Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Last active July 7, 2023 13:15
Show Gist options
  • Save paulofreitas/08ea4f2f09102df8630b8a3c8d7a41bb to your computer and use it in GitHub Desktop.
Save paulofreitas/08ea4f2f09102df8630b8a3c8d7a41bb to your computer and use it in GitHub Desktop.
Extending the default Eloquent User Provider (Laravel 5.4+)
<?php // config/auth.php
return [
/* ... */
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\User::class,
],
],
/* ... */
];
<?php
namespace App\Providers;
use App\Auth\UserProvider;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
$this->app->auth->provider('custom', function ($app, array $config) {
return new UserProvider($app['hash'], $config['model']);
});
}
}
<?php // app/Http/Controllers/Auth/LoginController.php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
// Make changes
return $request->only($this->username(), 'password');
}
}
<?php // app/Auth/UserProvider.php
namespace App\Auth;
use Illuminate\Support\Str;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\UserProvider as UserProviderContract;
class UserProvider extends EloquentUserProvider implements UserProviderContract
{
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
// Make changes
if (empty($credentials)) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
$query->where($key, $value);
}
}
return $query->first();
}
}
@mbpcoder
Copy link

Hi,
thinks. it was helpful.
you missed a ")" in AuthServiceProvider.php line 28

@GuillaumeLeon
Copy link

Thanks I implemented your code for my need (only to change the default password by a different name) and it worked

@inap-bannai
Copy link

Awesome! Thank you so much for shring!

@raffaele-capasso
Copy link

I've just tested right now, and I have to say it's work like a charm!
@paulofreitas thanks for sharing.

@Kwaadpepper
Copy link

Thanks for this tip 👍

@cyrilregal
Copy link

Anyone know if is this way work also for laravel 5.1 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment