Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@ozzargueyo

This comment has been minimized.

Copy link

@ozzargueyo ozzargueyo commented Jan 7, 2017

Hi. I think this is the best tutorial about this topic. I'm kind of lost about the Auth part and where to keep the session. I have an application with a api-login authentication. We check with a username and password to it and return null if not a valid user or an array with username,name and an array of 'roles'. With those values I need to login in my app. I don't have an user model or user table in my db. How can i achieve this? Please I think i'm not that lost but i can't figure it out.

Thanks in advance

@acountt

This comment has been minimized.

Copy link

@acountt acountt commented Jan 11, 2017

Hai, i think its is best tutorial .but Its not working in my project.how can i passed providers in my file.my path directory is app\Modules\Bootsgrid\Controllers--->please help me.

@arp690

This comment has been minimized.

Copy link

@arp690 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

This comment has been minimized.

Copy link

@vwasteels vwasteels commented Sep 14, 2017

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

This comment has been minimized.

Copy link

@CHOMNANP CHOMNANP commented Sep 24, 2017

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

@albertStaalburger

This comment has been minimized.

Copy link

@albertStaalburger albertStaalburger commented Dec 8, 2017

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

@kevindesousa

This comment has been minimized.

Copy link

@kevindesousa 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)

@comniemeer

This comment has been minimized.

Copy link

@comniemeer comniemeer commented Mar 10, 2018

Could you please update this to 5.6?

@ernarvaezm

This comment has been minimized.

Copy link

@ernarvaezm ernarvaezm commented Sep 8, 2018

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
You can’t perform that action at this time.