Guest User

Untitled

a guest
Jan 18th, 2019
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. https://domain.com/index.php/rest/V1/integration/customer/token?username=test@gmail.com&password=12345
  2.  
  3. protected function getCustomerToken($emailId){
  4. /**
  5. * @var MagentoCustomerModelCustomer $customer */
  6. */
  7. $customer->loadByEmail($emailId);
  8. if($customer->getId()){
  9. /**
  10. * @var MagentoIntegrationModelOauthTokenFactory $tokenModelFactory
  11. */
  12. $customerToken = $this->tokenModelFactory->create();
  13. $tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
  14. return $tokenKey;
  15. }
  16. return "YOU MSG FOR CUSTOMER NOT FOUND";
  17. }
  18.  
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function createCustomerAccessToken($username, $password)
  23. {
  24. $this->validatorHelper->validate($username, $password);
  25. $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER);
  26. try {
  27. $customerDataObject = $this->accountManagement->authenticate($username, $password);
  28. } catch (Exception $e) {
  29. $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER);
  30. throw new AuthenticationException(
  31. __('You did not sign in correctly or your account is temporarily disabled.')
  32. );
  33. }
  34. $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
  35. return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
  36. }
  37.  
  38. <?php
  39.  
  40.  
  41. namespace TestModuleControllerTest;
  42.  
  43. use MagentoCustomerModelSession;
  44. use MagentoFrameworkAppActionContext;
  45.  
  46.  
  47. class Token extends MagentoCustomerControllerAbstractAccount
  48. {
  49. /**
  50. * @var MagentoCustomerModelSession
  51. */
  52. protected $_customerSession;
  53.  
  54. public function __construct(
  55. Context $context,
  56. Session $customerSession,
  57. MagentoIntegrationModelOauthTokenFactory $tokenModelFactory
  58. ) {
  59. $this->_customerSession = $customerSession;
  60. $this->_tokenModelFactory = $tokenModelFactory;
  61. parent::__construct(
  62. $context
  63. );
  64. }
  65.  
  66. public function execute()
  67. {
  68. $customerId = $this->_customerSession->getCustomer()->getId();
  69. $customerToken = $this->_tokenModelFactory->create();
  70. echo "Customer-token=> ".$tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
  71. }
  72. }
  73.  
  74. class AutoLoginManagement implements FloCoreApiAutoLoginManagementInterface
  75. {
  76.  
  77. protected $_customer;
  78. protected $_customerSession;
  79. protected $_tokenModelFactory;
  80.  
  81. public function __construct(
  82. MagentoIntegrationModelOauthTokenFactory $tokenModelFactory,
  83. MagentoCustomerModelCustomer $customer,
  84. MagentoCustomerModelSession $customerSession
  85. )
  86. {
  87. $this->_customer = $customer;
  88. $this->_customerSession = $customerSession;
  89. $this->_tokenModelFactory = $tokenModelFactory;
  90. }
  91.  
  92. public function postAutoLogin($data)
  93. {
  94.  
  95. $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  96. // Load customer
  97. $customer = $objectManager->create('MagentoCustomerModelCustomer')->load($data['customer_id']);
  98.  
  99. if(! $customer->getId()) {
  100. return 'Not Found';
  101. } else {
  102. // Load customer session
  103. $customerSession = $objectManager->create('MagentoCustomerModelSession');
  104. $customerSession->setCustomerAsLoggedIn($customer);
  105.  
  106. $customerToken = $this->_tokenModelFactory->create();
  107. $tokenKey = $customerToken->createCustomerToken($customer->getId())->getToken();
  108. return $tokenKey;
  109.  
  110. }
  111.  
  112. }
  113.  
  114. private function getCustomerToken($customerId)
  115. {
  116. $customerToken = $this->tokenModelFactory->create();
  117. $tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
  118. return $tokenKey;
  119. }
  120. }
Add Comment
Please, Sign In to add comment