diff --git a/AuthenticationException.php b/AuthenticationException.php new file mode 100644 index 000000000..142afd1c8 --- /dev/null +++ b/AuthenticationException.php @@ -0,0 +1,37 @@ +guard = $guard; + + parent::__construct('Unauthenticated.'); + } + + /** + * Get the guard instance. + * + * @return \Illuminate\Contracts\Auth\Guard|null + */ + public function guard() + { + return $this->guard; + } +} diff --git a/GuardHelpers.php b/GuardHelpers.php index 1785904f3..95fee3ae7 100644 --- a/GuardHelpers.php +++ b/GuardHelpers.php @@ -23,6 +23,22 @@ trait GuardHelpers */ protected $provider; + /** + * Determine if the current user is authenticated. + * + * @return \Illuminate\Contracts\Auth\Authenticatable + * + * @throws \Illuminate\Auth\AuthenticationException + */ + public function authenticate() + { + if (! is_null($user = $this->user())) { + return $user; + } + + throw new AuthenticationException($this); + } + /** * Determine if the current user is authenticated. * @@ -59,10 +75,12 @@ public function id() * Set the current user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user - * @return void + * @return $this */ public function setUser(AuthenticatableContract $user) { $this->user = $user; + + return $this; } } diff --git a/SessionGuard.php b/SessionGuard.php index 3f522239b..a744962e7 100644 --- a/SessionGuard.php +++ b/SessionGuard.php @@ -691,13 +691,15 @@ public function getUser() * Set the current user. * * @param \Illuminate\Contracts\Auth\Authenticatable $user - * @return void + * @return $this */ public function setUser(AuthenticatableContract $user) { $this->user = $user; $this->loggedOut = false; + + return $this; } /**