GenerateCustomerToken.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\CustomerGraphQl\Model\Resolver;
  8. use Magento\Framework\Exception\AuthenticationException;
  9. use Magento\Framework\GraphQl\Config\Element\Field;
  10. use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
  11. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  12. use Magento\Framework\GraphQl\Query\ResolverInterface;
  13. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  14. use Magento\Integration\Api\CustomerTokenServiceInterface;
  15. /**
  16. * Customers Token resolver, used for GraphQL request processing.
  17. */
  18. class GenerateCustomerToken implements ResolverInterface
  19. {
  20. /**
  21. * @var CustomerTokenServiceInterface
  22. */
  23. private $customerTokenService;
  24. /**
  25. * @param CustomerTokenServiceInterface $customerTokenService
  26. */
  27. public function __construct(
  28. CustomerTokenServiceInterface $customerTokenService
  29. ) {
  30. $this->customerTokenService = $customerTokenService;
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function resolve(
  36. Field $field,
  37. $context,
  38. ResolveInfo $info,
  39. array $value = null,
  40. array $args = null
  41. ) {
  42. if (!isset($args['email'])) {
  43. throw new GraphQlInputException(__('Specify the "email" value.'));
  44. }
  45. if (!isset($args['password'])) {
  46. throw new GraphQlInputException(__('Specify the "password" value.'));
  47. }
  48. try {
  49. $token = $this->customerTokenService->createCustomerAccessToken($args['email'], $args['password']);
  50. return ['token' => $token];
  51. } catch (AuthenticationException $e) {
  52. throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
  53. }
  54. }
  55. }