The start of something beautiful
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Response;
|
||||
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* JWTAuthenticationFailureResponse.
|
||||
*
|
||||
* Response sent on failed JWT authentication (can be replaced by a custom Response).
|
||||
*
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
final class JWTAuthenticationFailureResponse extends JsonResponse
|
||||
{
|
||||
private string $message;
|
||||
|
||||
public function __construct(string $message = 'Bad credentials', int $statusCode = Response::HTTP_UNAUTHORIZED)
|
||||
{
|
||||
$this->message = $message;
|
||||
|
||||
parent::__construct(null, $statusCode, ['WWW-Authenticate' => 'Bearer']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response data with the statusCode & message included.
|
||||
*/
|
||||
public function setData(mixed $data = []): static
|
||||
{
|
||||
return parent::setData((array)$data + ["code" => $this->statusCode, "message" => $this->getMessage()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the failure message.
|
||||
*/
|
||||
public function setMessage(string $message): JWTAuthenticationFailureResponse
|
||||
{
|
||||
$this->message = $message;
|
||||
|
||||
$this->setData();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the failure message.
|
||||
*/
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Response;
|
||||
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
/**
|
||||
* Response sent on successful JWT authentication.
|
||||
*
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
final class JWTAuthenticationSuccessResponse extends JsonResponse
|
||||
{
|
||||
/**
|
||||
* @param string $token Json Web Token
|
||||
* @param array $data Extra data passed to the response
|
||||
*/
|
||||
public function __construct(string $token, array $data = [], array $jwtCookies = [])
|
||||
{
|
||||
if (!$jwtCookies) {
|
||||
parent::__construct(['token' => $token] + $data);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
parent::__construct($data);
|
||||
|
||||
foreach ($jwtCookies as $cookie) {
|
||||
$this->headers->setCookie($cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user