|
| 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 | +} |
0 commit comments