diff --git a/src/Illuminate/Auth/GuardHelpers.php b/src/Illuminate/Auth/GuardHelpers.php index 8dd5769f45de..95fee3ae7ccf 100644 --- a/src/Illuminate/Auth/GuardHelpers.php +++ b/src/Illuminate/Auth/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. * diff --git a/tests/Auth/AuthGuardTest.php b/tests/Auth/AuthGuardTest.php index 038e48d6741e..23bba2b22913 100755 --- a/tests/Auth/AuthGuardTest.php +++ b/tests/Auth/AuthGuardTest.php @@ -118,6 +118,25 @@ public function testLoginFiresLoginEvent() $mock->login($user); } + public function testAuthenticateReturnsUserWhenUserIsNotNull() + { + $user = m::mock('Illuminate\Contracts\Auth\Authenticatable'); + $guard = $this->getGuard()->setUser($user); + + $this->assertEquals($user, $guard->authenticate()); + } + + /** + * @expectedException \Illuminate\Auth\AuthenticationException + */ + public function testAuthenticateThrowsWhenUserIsNull() + { + $guard = $this->getGuard(); + $guard->getSession()->shouldReceive('get')->once()->andReturn(null); + + $guard->authenticate(); + } + public function testIsAuthedReturnsTrueWhenUserIsNotNull() { $user = m::mock('Illuminate\Contracts\Auth\Authenticatable');