Navigation Menu

Skip to content

Commit

Permalink
Restful api: authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
seregazhuk committed Apr 22, 2019
1 parent 7951465 commit 5b6ae61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion restulf-api-with-auth/src/Auth/Guard.php
Expand Up @@ -11,7 +11,7 @@ final class Guard

private $authenticator;

public function __construct($routesPattern, JwtAuthenticator $authenticator)
public function __construct(string $routesPattern, JwtAuthenticator $authenticator)
{
$this->routesPattern = $routesPattern;
$this->authenticator = $authenticator;
Expand Down
14 changes: 10 additions & 4 deletions restulf-api-with-auth/src/Controller/Login.php
Expand Up @@ -4,6 +4,7 @@

use App\Auth\JwtAuthenticator;
use App\JsonResponse;
use App\UserNotFoundError;
use Psr\Http\Message\ServerRequestInterface;

final class Login
Expand All @@ -17,9 +18,7 @@ public function __construct(JwtAuthenticator $authenticator)

public function __invoke(ServerRequestInterface $request)
{
$params = json_decode((string) $request->getBody(), true);
$email = $params['email'] ?? '';

$email = $this->extractEmail($request);
if ($email === null) {
return JsonResponse::badRequest("Field 'email' is required");
}
Expand All @@ -29,8 +28,15 @@ public function __invoke(ServerRequestInterface $request)
function (string $token) {
return JsonResponse::ok(['token' => $token]);
},
function () {
function (UserNotFoundError $error) {
return JsonResponse::unauthorized();
});
}

private function extractEmail(ServerRequestInterface $request): ?string
{
$params = json_decode((string)$request->getBody(), true);

return $params['email'] ?? '';
}
}

0 comments on commit 5b6ae61

Please sign in to comment.