Skip to content

Instantly share code, notes, and snippets.

@gbuckingham89
Last active August 22, 2022 12:01
Show Gist options
  • Save gbuckingham89/672114f782e747b8b99e to your computer and use it in GitHub Desktop.
Save gbuckingham89/672114f782e747b8b99e to your computer and use it in GitHub Desktop.
Blog: Laravel Authentication Customer User Provider Demo
'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
}
}
@arp690
Copy link

arp690 commented Aug 28, 2017

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!

@vwasteels
Copy link

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

@CHOMNANP
Copy link

Mine say that "Class 'App\Providers\UserProvider' not found.
I'm using Laravel 5.5

@albertStaalburger
Copy link

UserProvider.php is missing the include: use Illuminate\Contracts\Auth\Authenticatable;

@kevindesousa
Copy link

kevindesousa commented Feb 12, 2018

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)

@mtnair
Copy link

mtnair commented Mar 10, 2018

Could you please update this to 5.6?

@ernarvaezm
Copy link

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;

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