-
-
Save gbuckingham89/672114f782e747b8b99e to your computer and use it in GitHub Desktop.
| 'providers' => [ | |
| 'users' => [ | |
| 'driver' => 'our_provider', | |
| ], | |
| ], |
| <?php | |
| namespace App\Authentication; | |
| use Auth; | |
| use App\Authentication\UserProvider; | |
| use Illuminate\Support\ServiceProvider; | |
| class AuthServiceProvider extends ServiceProvider | |
| { | |
| /** | |
| * Perform post-registration booting of services. | |
| * | |
| * @return void | |
| */ | |
| public function boot() | |
| { | |
| Auth::provider('our_provider', function($app, array $config) { | |
| return new UserProvider(); | |
| }); | |
| } | |
| /** | |
| * Register bindings in the container. | |
| * | |
| * @return void | |
| */ | |
| public function register() | |
| { | |
| // | |
| } | |
| } |
| <?php | |
| namespace App\Authentication; | |
| use Illuminate\Contracts\Auth\Authenticatable; | |
| class User implements Authenticatable | |
| { | |
| /** | |
| * @return string | |
| */ | |
| public function getAuthIdentifierName() | |
| { | |
| // Return the name of unique identifier for the user (e.g. "id") | |
| } | |
| /** | |
| * @return mixed | |
| */ | |
| public function getAuthIdentifier() | |
| { | |
| // Return the unique identifier for the user (e.g. their ID, 123) | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getAuthPassword() | |
| { | |
| // Returns the (hashed) password for the user | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getRememberToken() | |
| { | |
| // Return the token used for the "remember me" functionality | |
| } | |
| /** | |
| * @param string $value | |
| * @return void | |
| */ | |
| public function setRememberToken($value) | |
| { | |
| // Save a new token user for the "remember me" functionality | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getRememberTokenName() | |
| { | |
| // Return the name of the column / attribute used to store the "remember me" token | |
| } | |
| } |
| <?php | |
| namespace App\Authentication; | |
| use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider; | |
| class UserProvider implements IlluminateUserProvider | |
| { | |
| /** | |
| * @param mixed $identifier | |
| * @return \Illuminate\Contracts\Auth\Authenticatable|null | |
| */ | |
| public function retrieveById($identifier) | |
| { | |
| // Get and return a user by their unique identifier | |
| } | |
| /** | |
| * @param mixed $identifier | |
| * @param string $token | |
| * @return \Illuminate\Contracts\Auth\Authenticatable|null | |
| */ | |
| public function retrieveByToken($identifier, $token) | |
| { | |
| // Get and return a user by their unique identifier and "remember me" token | |
| } | |
| /** | |
| * @param \Illuminate\Contracts\Auth\Authenticatable $user | |
| * @param string $token | |
| * @return void | |
| */ | |
| public function updateRememberToken(Authenticatable $user, $token) | |
| { | |
| // Save the given "remember me" token for the given user | |
| } | |
| /** | |
| * Retrieve a user by the given credentials. | |
| * | |
| * @param array $credentials | |
| * @return \Illuminate\Contracts\Auth\Authenticatable|null | |
| */ | |
| public function retrieveByCredentials(array $credentials) | |
| { | |
| // Get and return a user by looking up the given credentials | |
| } | |
| /** | |
| * Validate a user against the given credentials. | |
| * | |
| * @param \Illuminate\Contracts\Auth\Authenticatable $user | |
| * @param array $credentials | |
| * @return bool | |
| */ | |
| public function validateCredentials(Authenticatable $user, array $credentials) | |
| { | |
| // Check that given credentials belong to the given user | |
| } | |
| } |
Hello,
I'm using this architecture for my project,
I have a simple question, how do you access the UserProvider instance ?
I'm trying to get a user by ID...
Thanks
Mine say that "Class 'App\Providers\UserProvider' not found.
I'm using Laravel 5.5
UserProvider.php is missing the include: use Illuminate\Contracts\Auth\Authenticatable;
I'm using 5.5 and I have :
Declaration of App\Authentication\UserProvider::updateRememberToken(App\Authentication\Authenticatable $user, $token) must be compatible with Illuminate\Contracts\Auth\UserProvider::updateRememberToken(Illuminate\Contracts\Auth\Authenticatable $user, $token)
Could you please update this to 5.6?
Declaration of App\Authentication\UserProvider::updateRememberToken(App\Authentication\Authenticatable $user, $token) must be compatible with Illuminate\Contracts\Auth\UserProvider::updateRememberToken(Illuminate\Contracts\Auth\Authenticatable $user, $token)
I fixed it by adding this line:
use Illuminate\Contracts\Auth\Authenticatable;
This is awesome! thanks for the help, I'm trying to do the same in Laravel 5.4 but I get an error in AuthServiceProvider.php Argument 1 passed to App\Authentication\UserProvider::__construct() must be an instance of Illuminate\Contracts\Hashing\Hasher, none given, called in app\Providers\AuthServiceProvider.php on line 32 and defined', 'app\Authentication\UserProvider.php I think it has something to do with the new registerPolicies but I don't know how to implement this. Could you help me with an example of how to create the AuthServiceProvider boot function in Laravel 5.4? thanks!