Last active
December 15, 2015 02:28
-
-
Save luisdalmolin/5187080 to your computer and use it in GitHub Desktop.
Some thoughts about the Laravel Auth. The only real changes is the new method getModel in EloquentUserProvider (need to add in DatabaseUserProvider too), and the methods `getName` and `getRecallerName` in the `Guard` class, to make the session name with the related model.
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 Commerce\Client\Facades; | |
class ClientAuth | |
{ | |
/** | |
* @var Illuminate\Auth\Guard | |
*/ | |
static $guard; | |
public static function guest() | |
{ | |
return static::$guard->guest(); | |
} | |
public static function check() | |
{ | |
return static::$guard->check(); | |
} | |
public static function user() | |
{ | |
return static::$guard->user(); | |
} | |
public static function attempt(array $credentials = array(), $remember = false, $login = true) | |
{ | |
return static::$guard->attempt($credentials, $remember, $login); | |
} | |
public static function logout() | |
{ | |
return static::$guard->logout(); | |
} | |
} |
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 | |
/** | |
* Then, I can do something like this class, and the ClientAuth class | |
*/ | |
namespace Commerce\Client; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Auth\Guard; | |
use Illuminate\Auth\EloquentUserProvider; | |
use Illuminate\Hashing\BcryptHasher; | |
use Commerce\Client\Facades\ClientAuth; | |
class ClientServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
$this->registerAuthEvents(); | |
} | |
/** | |
* Register the service provider. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$provider = new EloquentUserProvider(new BcryptHasher, 'Client'); | |
$guard = new Guard( | |
$provider, | |
$this->app['session'] | |
); | |
$guard->setCookieJar($this->app['cookie']); | |
ClientAuth::$guard = $guard; | |
} | |
protected function registerAuthEvents() | |
{ | |
$app = $this->app; | |
$app->after(function($request, $response) use ($app) | |
{ | |
foreach (ClientAuth::$guard->getQueuedCookies() as $cookie) { | |
$response->headers->setCookie($cookie); | |
} | |
}); | |
} | |
} |
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 Illuminate\Auth; | |
use Illuminate\Hashing\HasherInterface; | |
class EloquentUserProvider implements UserProviderInterface { | |
/** | |
* The Eloquent user model. | |
* | |
* @var string | |
*/ | |
protected $model; | |
/** | |
* Returning the model name | |
* | |
* @return string | |
*/ | |
public function getModel() | |
{ | |
return $this->model; | |
} | |
} |
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 Illuminate\Auth; | |
use Illuminate\Cookie\CookieJar; | |
use Illuminate\Events\Dispatcher; | |
use Illuminate\Encryption\Encrypter; | |
use Symfony\Component\HttpFoundation\Request; | |
use Illuminate\Session\Store as SessionStore; | |
class Guard { | |
/** | |
* Get a unique identifier for the auth session value. | |
* | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'login_'.md5($this->provider->getModel()); // instead of using Guard as getName(), use the name of the model | |
} | |
/** | |
* Get the name of the cookie used to store the "recaller". | |
* | |
* @return string | |
*/ | |
public function getRecallerName() | |
{ | |
return 'remember_'.md5($this->provider->getModel()); // instead of using Guard as getRecallerName(), use the name of the model | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment