-
-
Save kevnk/cdd5f1244d3d7abcde4edd98280d8ca2 to your computer and use it in GitHub Desktop.
Extending the default EloquentUserProvider (Laravel 9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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\Models\User::class, | |
], | |
], | |
/* ... */ | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers\Auth; | |
use Closure; | |
use Illuminate\Contracts\Auth\Authenticatable as UserContract; | |
use Illuminate\Contracts\Auth\UserProvider; | |
use Illuminate\Contracts\Hashing\Hasher as HasherContract; | |
use Illuminate\Contracts\Support\Arrayable; | |
use Illuminate\Support\Str; | |
use Illuminate\Auth\EloquentUserProvider as BaseEloquentUserProvider; | |
class EloquentUserProvider extends BaseEloquentUserProvider | |
{ | |
/** | |
* Retrieve a user by the given credentials. | |
* | |
* @param array $credentials | |
* @return \Illuminate\Contracts\Auth\Authenticatable|null | |
*/ | |
public function retrieveByCredentials(array $credentials) | |
{ | |
if ( | |
empty($credentials) || | |
(count($credentials) === 1 && | |
Str::contains($this->firstCredentialKey($credentials), 'password')) | |
) { | |
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->newModelQuery(); | |
foreach ($credentials as $key => $value) { | |
if (Str::contains($key, 'password')) { | |
continue; | |
} | |
if (is_array($value) || $value instanceof Arrayable) { | |
$query->whereIn($key, $value); | |
} elseif ($value instanceof Closure) { | |
$value($query); | |
} else { | |
$query->where($key, $value); | |
} | |
} | |
return $query->first(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this to encrypt user emails.
php artisan make:migration add_email_key_to_users
app/Models/User.php
app/Providers/Auth/EloquentUserProvider.php:40