Last active
July 7, 2023 13:15
-
-
Save paulofreitas/08ea4f2f09102df8630b8a3c8d7a41bb to your computer and use it in GitHub Desktop.
Extending the default Eloquent User Provider (Laravel 5.4+)
This file contains 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 // 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, | |
], | |
], | |
/* ... */ | |
]; |
This file contains 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 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 // 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'); | |
} | |
} |
This file contains 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 // 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(); | |
} | |
} |
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
Thanks for this tip 👍