Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example authentication driver for Laravel 4.
<?php namespace Project\Providers;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Auth\GenericUser;
class AuthUserProvider implements UserProviderInterface {
/**
* External webservice for authentication
*/
private $webservice;
/**
* The user object.
*/
private $user;
/**
* Constructor
*
* @return void
*/
public function __construct(\Project\Webservice\Auth $webservice)
{
$this->webservice = $webservice;
$this->user = null;
}
/**
* Retrieves a user by id
*
* @param int $identifier
* @return mixed null|array
*/
public function retrieveByID($identifier)
{
$this->user = is_null($this->user) ? $this->webservice->find($identifier) : $this->user;
return $this->user;
}
/**
* Tries to find a user based on the credentials passed.
*
* @param array $crendtials username|password
* @return mixed bool|UserInterface
*/
public function retrieveByCredentials(array $credentials)
{
if(!$user = $this->webservice->find($credentials['username'])) return false;
return new GenericUser($user);
}
/**
* Validates the credentials passed to the ones in webservice.
*
* @param UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
return $this->webservice->validateCredentials($credentials['password'], $user->password);
}
}
helmut commented Dec 10, 2013

Should be $this->webservice in last method...

Owner

@helmut: You are absolutely correct. Thanks, fixed it now.

Thanks for the code!
Just a late edit - retrieveByCredentials should return null instead of false on line 53

Thanks for you tutorial, but I still don't get what is your $webservice variable. What does it mean? Can you show an example of it, please?

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