Skip to content

Commit 527e4fe

Browse files
committed
GraphQl-129: Retrieve Customer token
1 parent 232eec2 commit 527e4fe

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver\Customer\Account;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\Customer\Model\Customer;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\Framework\GraphQl\Query\Resolver\Value;
16+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
17+
use Magento\Framework\GraphQl\Query\ResolverInterface;
18+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
19+
20+
class GenerateCustomerToken implements ResolverInterface
21+
{
22+
/**
23+
* @var UserContextInterface
24+
*/
25+
private $userContext;
26+
27+
/**
28+
* @var CustomerTokenServiceInterface
29+
*/
30+
private $customerTokenService;
31+
32+
/**
33+
* @var ValueFactory
34+
*/
35+
private $valueFactory;
36+
37+
/**
38+
* @param UserContextInterface $userContext
39+
* @param CustomerTokenServiceInterface $customerTokenService
40+
* @param ValueFactory $valueFactory
41+
*/
42+
public function __construct(
43+
UserContextInterface $userContext,
44+
CustomerTokenServiceInterface $customerTokenService,
45+
ValueFactory $valueFactory
46+
) {
47+
$this->userContext = $userContext;
48+
$this->customerTokenService = $customerTokenService;
49+
$this->valueFactory = $valueFactory;
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
public function resolve(
56+
Field $field,
57+
$context,
58+
ResolveInfo $info,
59+
array $value = null,
60+
array $args = null
61+
): Value {
62+
63+
$token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']);
64+
//TODO: exception
65+
$result = function () use ($token) {
66+
return !empty($token) ? $token : '';
67+
};
68+
return $this->valueFactory->create($result);
69+
}
70+
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ type Query {
55
customer: Customer @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\Customer") @doc(description: "The customer query returns information about a customer account")
66
}
77

8+
type Mutation {
9+
generateCustomerToken(email: String!, password: String!): GenerateCustomerTokenOutput! @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\Customer\\Account\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
10+
}
11+
12+
type GenerateCustomerTokenOutput {
13+
token: String! @doc(description: "The customer token")
14+
}
15+
816
type Customer @doc(description: "Customer defines the customer name and address and other details") {
917
created_at: String @doc(description: "Timestamp indicating when the account was created")
1018
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)")

0 commit comments

Comments
 (0)