yiisoft /
demo-api
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace App\Auth; |
||
| 6 | |||
| 7 | use App\User\UserRequest; |
||
| 8 | use App\User\UserService; |
||
| 9 | use Psr\Http\Message\ResponseInterface; |
||
| 10 | use Yiisoft\DataResponse\DataResponseFactoryInterface as ResponseFactory; |
||
| 11 | use OpenApi\Annotations as OA; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @OA\Tag( |
||
| 15 | * name="auth", |
||
| 16 | * description="Authentication" |
||
| 17 | * ) |
||
| 18 | * |
||
| 19 | * @OA\SecurityScheme( |
||
| 20 | * securityScheme="ApiKey", |
||
| 21 | * type="apiKey", |
||
| 22 | * in="header", |
||
| 23 | * name="X-Api-Key" |
||
| 24 | * ) |
||
| 25 | */ |
||
| 26 | final class AuthController |
||
| 27 | { |
||
| 28 | private ResponseFactory $responseFactory; |
||
| 29 | private UserService $userService; |
||
| 30 | |||
| 31 | 2 | public function __construct( |
|
| 32 | ResponseFactory $responseFactory, |
||
| 33 | UserService $userService |
||
| 34 | ) { |
||
| 35 | 2 | $this->responseFactory = $responseFactory; |
|
| 36 | 2 | $this->userService = $userService; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @OA\Post( |
||
| 41 | * tags={"auth"}, |
||
| 42 | * path="/auth/", |
||
| 43 | * summary="Authenticate by params", |
||
| 44 | * description="", |
||
| 45 | * @OA\Response( |
||
| 46 | * response="200", |
||
| 47 | * description="Success", |
||
| 48 | * @OA\JsonContent( |
||
| 49 | * allOf={ |
||
| 50 | * @OA\Schema(ref="#/components/schemas/Response"), |
||
| 51 | * @OA\Schema( |
||
| 52 | * @OA\Property( |
||
| 53 | * property="data", |
||
| 54 | * type="object", |
||
| 55 | * @OA\Property(property="token", format="string", example="uap4X5Bd7078lxIFvxAflcGAa5D95iSSZkNjg3XFrE2EBRBlbj"), |
||
| 56 | * ), |
||
| 57 | * ), |
||
| 58 | * }, |
||
| 59 | * ) |
||
| 60 | * ), |
||
| 61 | * @OA\Response( |
||
| 62 | * response="400", |
||
| 63 | * description="Bad request", |
||
| 64 | * @OA\JsonContent(ref="#/components/schemas/BadResponse") |
||
| 65 | * ), |
||
| 66 | * @OA\RequestBody( |
||
| 67 | * required=true, |
||
| 68 | * @OA\MediaType( |
||
| 69 | * mediaType="application/json", |
||
| 70 | * @OA\Schema(ref="#/components/schemas/AuthRequest"), |
||
| 71 | * ), |
||
| 72 | * ), |
||
| 73 | * ) |
||
| 74 | */ |
||
| 75 | 1 | public function login(AuthRequest $request): ResponseInterface |
|
| 76 | { |
||
| 77 | 1 | return $this->responseFactory->createResponse( |
|
| 78 | [ |
||
| 79 | 1 | 'token' => $this->userService |
|
| 80 | 1 | ->login( |
|
| 81 | 1 | $request->getLogin(), |
|
| 82 | 1 | $request->getPassword() |
|
| 83 | ) |
||
| 84 | 1 | ->getToken(), |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 85 | ] |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @OA\Post( |
||
| 91 | * tags={"auth"}, |
||
| 92 | * path="/logout/", |
||
| 93 | * summary="Logout", |
||
| 94 | * description="", |
||
| 95 | * security={{"ApiKey": {}}}, |
||
| 96 | * @OA\Response( |
||
| 97 | * response="200", |
||
| 98 | * description="Success", |
||
| 99 | * @OA\JsonContent(ref="#/components/schemas/Response") |
||
| 100 | * ), |
||
| 101 | * @OA\Response( |
||
| 102 | * response="400", |
||
| 103 | * description="Bad request", |
||
| 104 | * @OA\JsonContent(ref="#/components/schemas/BadResponse") |
||
| 105 | * ), |
||
| 106 | * ) |
||
| 107 | */ |
||
| 108 | 1 | public function logout(UserRequest $request): ResponseInterface |
|
| 109 | { |
||
| 110 | 1 | $this->userService->logout($request->getUser()); |
|
| 111 | 1 | return $this->responseFactory->createResponse(); |
|
| 112 | } |
||
| 113 | } |
||
| 114 |