diff --git a/doc/swagger.yml b/doc/swagger.yml index a09b00c27..588eae177 100644 --- a/doc/swagger.yml +++ b/doc/swagger.yml @@ -756,7 +756,7 @@ paths: - "users" summary: "Request resetting password of user with passed email." description: "Email with reset password path will be sent to user. Default path for password resetting is `/password-reset/{token}`. To change it, you need to override template `@SyliusShopApi\\Email\\passwordReset.html.twig`." - opearationId: "requestPasswordReset" + operationId: "requestPasswordReset" consumes: - "application/json" parameters: @@ -764,13 +764,26 @@ paths: in: "body" description: "Email of user which want to reset password." required: true - schama: + schema: $ref: "#/definitions/RequestPasswordResetting" responses: 204: description: "Reset password request has been sent." 500: description: "User with provided email has not been found." + /me: + get: + tags: + - "users" + summary: "Provides currently logged in user details." + operationId: "me" + responses: + 200: + schema: + type: "object" + $ref: "#/definitions/LoggedInCustomerDetails" + 500: + description: "There is no currently logged in user." definitions: PickupCartRequest: @@ -1330,6 +1343,18 @@ definitions: email: type: "string" example: "test@example.com" + LoggedInCustomerDetails: + type: "object" + properties: + firstName: + type: "string" + example: "Sherlock" + lastName: + type: "string" + example: "Holmes" + email: + type: "string" + example: "sherlock@holmes.com" GeneralError: type: "object" properties: diff --git a/src/Controller/Customer/LoggedInCustomerDetailsAction.php b/src/Controller/Customer/LoggedInCustomerDetailsAction.php new file mode 100644 index 000000000..ee72b45da --- /dev/null +++ b/src/Controller/Customer/LoggedInCustomerDetailsAction.php @@ -0,0 +1,52 @@ +viewHandler = $viewHandler; + $this->tokenStorage = $tokenStorage; + } + + public function __invoke(Request $request): Response + { + /** @var ShopUserInterface $user */ + $user = $this->tokenStorage->getToken()->getUser(); + + Assert::isInstanceOf($user, ShopUserInterface::class); + + $customer = $user->getCustomer(); + + return $this->viewHandler->handle(View::create([ + 'firstName' => $customer->getFirstName(), + 'lastName' => $customer->getLastName(), + 'email' => $customer->getEmail(), + ], Response::HTTP_OK)); + } +} diff --git a/src/Resources/config/routing.yml b/src/Resources/config/routing.yml index ebe41ba2d..9a4875bbc 100644 --- a/src/Resources/config/routing.yml +++ b/src/Resources/config/routing.yml @@ -21,3 +21,7 @@ shop_api_register: shop_api_checkout: resource: "@ShopApiPlugin/Resources/config/routing/checkout.yml" prefix: /shop-api/checkout + +shop_api_customer: + resource: "@ShopApiPlugin/Resources/config/routing/customer.yml" + prefix: /shop-api diff --git a/src/Resources/config/routing/customer.yml b/src/Resources/config/routing/customer.yml new file mode 100644 index 000000000..1c05c5b7c --- /dev/null +++ b/src/Resources/config/routing/customer.yml @@ -0,0 +1,5 @@ +shop_api_me: + path: /me + methods: [GET] + defaults: + _controller: sylius.shop_api_plugin.controller.customer.logged_in_customer_details_action diff --git a/src/Resources/config/services/actions/customer.xml b/src/Resources/config/services/actions/customer.xml index e0105a6c0..c248dbe66 100644 --- a/src/Resources/config/services/actions/customer.xml +++ b/src/Resources/config/services/actions/customer.xml @@ -26,5 +26,12 @@ + + + + + diff --git a/tests/Controller/LoggedInCustomerDetailsActionTest.php b/tests/Controller/LoggedInCustomerDetailsActionTest.php new file mode 100644 index 000000000..ff006439b --- /dev/null +++ b/tests/Controller/LoggedInCustomerDetailsActionTest.php @@ -0,0 +1,40 @@ +loadFixturesFromFile('customer.yml'); + + $data = +<<client->request('POST', '/shop-api/login_check', [], [], ['CONTENT_TYPE' => 'application/json', 'ACCEPT' => 'application/json'], $data); + + $response = json_decode($this->client->getResponse()->getContent(), true); + $this->client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $response['token'])); + + $this->client->request('GET', '/shop-api/me', [], [], [ + 'CONTENT_TYPE' => 'application/json', + 'ACCEPT' => 'application/json', + ]); + + $response = $this->client->getResponse(); + $this->assertResponse($response, 'customer/logged_in_customer_details_response', Response::HTTP_OK); + } +} diff --git a/tests/Responses/Expected/customer/logged_in_customer_details_response.json b/tests/Responses/Expected/customer/logged_in_customer_details_response.json new file mode 100644 index 000000000..9e3e74acf --- /dev/null +++ b/tests/Responses/Expected/customer/logged_in_customer_details_response.json @@ -0,0 +1,5 @@ +{ + "firstName": "Oliver", + "lastName": "Queen", + "email": "oliver@queen.com" +}