Skip to content

Commit

Permalink
Add authenticate method to the guards
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed May 22, 2016
1 parent cc250f4 commit 1fd2064
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Auth/GuardHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 1fd2064

Please sign in to comment.