Skip to content

Commit

Permalink
[5.2] Add authenticate method to the guards (#13651)
Browse files Browse the repository at this point in the history
* Make setUser fluent

* Create AuthenticationException

* Add authenticate method to the guards

* Convert an AuthenticationException into an unauthenticated response
  • Loading branch information
JosephSilber authored and taylorotwell committed May 23, 2016
1 parent 5f62411 commit 238b000
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
37 changes: 37 additions & 0 deletions AuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\Auth;

use Exception;

class AuthenticationException extends Exception
{
/**
* The guard instance.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;

/**
* Create a new authentication exception.
*
* @param \Illuminate\Contracts\Auth\Guard|null $guard
*/
public function __construct($guard = null)
{
$this->guard = $guard;

parent::__construct('Unauthenticated.');
}

/**
* Get the guard instance.
*
* @return \Illuminate\Contracts\Auth\Guard|null
*/
public function guard()
{
return $this->guard;
}
}
20 changes: 19 additions & 1 deletion 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 Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 238b000

Please sign in to comment.