diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/Account/GenerateCustomerToken.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/Account/GenerateCustomerToken.php new file mode 100644 index 0000000000000..b756a96411a44 --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/Account/GenerateCustomerToken.php @@ -0,0 +1,61 @@ +customerTokenService = $customerTokenService; + } + + /** + * @inheritdoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + try { + if (!isset($args['email'])) { + throw new GraphQlInputException(__('"email" value should be specified')); + } + if (!isset($args['password'])) { + throw new GraphQlInputException(__('"password" value should be specified')); + } + $token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']); + return ['token' => $token]; + } catch (AuthenticationException $e) { + throw new GraphQlAuthorizationException( + __($e->getMessage()) + ); + } + } +} diff --git a/app/code/Magento/CustomerGraphQl/composer.json b/app/code/Magento/CustomerGraphQl/composer.json index 290d925215ec2..c26c83c95be38 100644 --- a/app/code/Magento/CustomerGraphQl/composer.json +++ b/app/code/Magento/CustomerGraphQl/composer.json @@ -6,6 +6,7 @@ "php": "~7.1.3||~7.2.0", "magento/module-customer": "*", "magento/module-authorization": "*", + "magento/module-integration": "*", "magento/framework": "*" }, "suggest": { diff --git a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls index 91b7ef1f9be15..2fbf2b1219390 100644 --- a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls @@ -5,6 +5,14 @@ type Query { customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "The customer query returns information about a customer account") } +type Mutation { + generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token") +} + +type CustomerToken { + token: String @doc(description: "The customer token") +} + type Customer @doc(description: "Customer defines the customer name and address and other details") { created_at: String @doc(description: "Timestamp indicating when the account was created") group_id: Int @doc(description: "The group assigned to the user. Default values are 0 (Not logged in), 1 (General), 2 (Wholesale), and 3 (Retailer)") diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php new file mode 100644 index 0000000000000..ae28e23a28bf1 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php @@ -0,0 +1,71 @@ +graphQlQuery($mutation); + $this->assertArrayHasKey('generateCustomerToken', $response); + $this->assertInternalType('array', $response['generateCustomerToken']); + } + + /** + * Verify customer with invalid credentials + */ + public function testGenerateCustomerTokenWithInvalidCredentials() + { + $userName = 'customer@example.com'; + $password = 'bad-password'; + + $mutation + = <<expectException(\Exception::class); + $this->expectExceptionMessage('GraphQL response contains errors: The account sign-in' . ' ' . + 'was incorrect or your account is disabled temporarily. Please wait and try again later.'); + $this->graphQlQuery($mutation); + } +}