diff --git a/GuardHelpers.php b/GuardHelpers.php new file mode 100644 index 000000000..01e6cbd8e --- /dev/null +++ b/GuardHelpers.php @@ -0,0 +1,68 @@ +user()); + } + + /** + * Determine if the current user is a guest. + * + * @return bool + */ + public function guest() + { + return ! $this->check(); + } + + /** + * Get the ID for the currently authenticated user. + * + * @return int|null + */ + public function id() + { + if ($this->user()) { + return $this->user()->getAuthIdentifier(); + } + } + + /** + * Set the current user. + * + * @param \Illuminate\Contracts\Auth\Authenticatable $user + * @return void + */ + public function setUser(Authenticatable $user) + { + $this->user = $user; + } +} diff --git a/SessionGuard.php b/SessionGuard.php index c6efe47f7..52747a307 100644 --- a/SessionGuard.php +++ b/SessionGuard.php @@ -9,13 +9,15 @@ use Illuminate\Contracts\Auth\StatefulGuard; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Auth\SupportsBasicAuth; use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar; -use Illuminate\Contracts\Auth\Authenticatable as UserContract; use Symfony\Component\HttpFoundation\Session\SessionInterface; class SessionGuard implements StatefulGuard, SupportsBasicAuth { + use GuardHelpers; + /** * The name of the Guard. Typically "session". * @@ -25,13 +27,6 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth */ protected $name; - /** - * The currently authenticated user. - * - * @var \Illuminate\Contracts\Auth\Authenticatable - */ - protected $user; - /** * The user we last attempted to retrieve. * @@ -46,13 +41,6 @@ class SessionGuard implements StatefulGuard, SupportsBasicAuth */ protected $viaRemember = false; - /** - * The user provider implementation. - * - * @var \Illuminate\Contracts\Auth\UserProvider - */ - protected $provider; - /** * The session used by the guard. * @@ -115,26 +103,6 @@ public function __construct($name, $this->provider = $provider; } - /** - * Determine if the current user is authenticated. - * - * @return bool - */ - public function check() - { - return ! is_null($this->user()); - } - - /** - * Determine if the current user is a guest. - * - * @return bool - */ - public function guest() - { - return ! $this->check(); - } - /** * Get the currently authenticated user. * @@ -440,7 +408,7 @@ public function attempting($callback) * @param bool $remember * @return void */ - public function login(UserContract $user, $remember = false) + public function login(Authenticatable $user, $remember = false) { $this->updateSession($user->getAuthIdentifier()); @@ -527,7 +495,7 @@ public function onceUsingId($id) * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ - protected function queueRecallerCookie(UserContract $user) + protected function queueRecallerCookie(Authenticatable $user) { $value = $user->getAuthIdentifier().'|'.$user->getRememberToken(); @@ -597,7 +565,7 @@ protected function clearUserDataFromStorage() * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ - protected function refreshRememberToken(UserContract $user) + protected function refreshRememberToken(Authenticatable $user) { $user->setRememberToken($token = Str::random(60)); @@ -610,7 +578,7 @@ protected function refreshRememberToken(UserContract $user) * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ - protected function createRememberTokenIfDoesntExist(UserContract $user) + protected function createRememberTokenIfDoesntExist(Authenticatable $user) { if (empty($user->getRememberToken())) { $this->refreshRememberToken($user); @@ -712,7 +680,7 @@ public function getUser() * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ - public function setUser(UserContract $user) + public function setUser(Authenticatable $user) { $this->user = $user; diff --git a/TokenGuard.php b/TokenGuard.php index 923933303..63012f8ad 100644 --- a/TokenGuard.php +++ b/TokenGuard.php @@ -10,19 +10,7 @@ class TokenGuard implements Guard { - /** - * The currently authenticated user. - * - * @var \Illuminate\Contracts\Auth\Authenticatable - */ - protected $user; - - /** - * The user provider implementation. - * - * @var \Illuminate\Contracts\Auth\UserProvider - */ - protected $provider; + use GuardHelpers; /** * The request instance. @@ -65,26 +53,6 @@ public function __construct(UserProvider $provider, $this->storageKey = $storageKey; } - /** - * Determine if the current user is authenticated. - * - * @return bool - */ - public function check() - { - return ! is_null($this->user()); - } - - /** - * Determine if the current user is a guest. - * - * @return bool - */ - public function guest() - { - return ! $this->check(); - } - /** * Get the currently authenticated user. * @@ -126,28 +94,12 @@ protected function getTokenForRequest() } if (empty($token)) { - $header = $this->request->header('Authorization'); - - if (Str::startsWith($header, 'Bearer ')) { - $token = Str::substr($header, 7); - } + $token = $this->request->bearerToken(); } return $token; } - /** - * Get the ID for the currently authenticated user. - * - * @return int|null - */ - public function id() - { - if ($this->user()) { - return $this->user()->getAuthIdentifier(); - } - } - /** * Validate a user's credentials. * @@ -177,17 +129,6 @@ public function logout() // } - /** - * Set the current user. - * - * @param \Illuminate\Contracts\Auth\Authenticatable $user - * @return void - */ - public function setUser(Authenticatable $user) - { - $this->user = $user; - } - /** * Set the current request instance. *