-
-
Save antonigiske/1baf1523ea7802a82404 to your computer and use it in GitHub Desktop.
Example authentication driver for Laravel 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 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: 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
Should be $this->webservice in last method...