Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://domain.com/index.php/rest/V1/integration/customer/token?username=test@gmail.com&password=12345
- protected function getCustomerToken($emailId){
- /**
- * @var MagentoCustomerModelCustomer $customer */
- */
- $customer->loadByEmail($emailId);
- if($customer->getId()){
- /**
- * @var MagentoIntegrationModelOauthTokenFactory $tokenModelFactory
- */
- $customerToken = $this->tokenModelFactory->create();
- $tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
- return $tokenKey;
- }
- return "YOU MSG FOR CUSTOMER NOT FOUND";
- }
- /**
- * {@inheritdoc}
- */
- public function createCustomerAccessToken($username, $password)
- {
- $this->validatorHelper->validate($username, $password);
- $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER);
- try {
- $customerDataObject = $this->accountManagement->authenticate($username, $password);
- } catch (Exception $e) {
- $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER);
- throw new AuthenticationException(
- __('You did not sign in correctly or your account is temporarily disabled.')
- );
- }
- $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
- return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
- }
- <?php
- namespace TestModuleControllerTest;
- use MagentoCustomerModelSession;
- use MagentoFrameworkAppActionContext;
- class Token extends MagentoCustomerControllerAbstractAccount
- {
- /**
- * @var MagentoCustomerModelSession
- */
- protected $_customerSession;
- public function __construct(
- Context $context,
- Session $customerSession,
- MagentoIntegrationModelOauthTokenFactory $tokenModelFactory
- ) {
- $this->_customerSession = $customerSession;
- $this->_tokenModelFactory = $tokenModelFactory;
- parent::__construct(
- $context
- );
- }
- public function execute()
- {
- $customerId = $this->_customerSession->getCustomer()->getId();
- $customerToken = $this->_tokenModelFactory->create();
- echo "Customer-token=> ".$tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
- }
- }
- class AutoLoginManagement implements FloCoreApiAutoLoginManagementInterface
- {
- protected $_customer;
- protected $_customerSession;
- protected $_tokenModelFactory;
- public function __construct(
- MagentoIntegrationModelOauthTokenFactory $tokenModelFactory,
- MagentoCustomerModelCustomer $customer,
- MagentoCustomerModelSession $customerSession
- )
- {
- $this->_customer = $customer;
- $this->_customerSession = $customerSession;
- $this->_tokenModelFactory = $tokenModelFactory;
- }
- public function postAutoLogin($data)
- {
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- // Load customer
- $customer = $objectManager->create('MagentoCustomerModelCustomer')->load($data['customer_id']);
- if(! $customer->getId()) {
- return 'Not Found';
- } else {
- // Load customer session
- $customerSession = $objectManager->create('MagentoCustomerModelSession');
- $customerSession->setCustomerAsLoggedIn($customer);
- $customerToken = $this->_tokenModelFactory->create();
- $tokenKey = $customerToken->createCustomerToken($customer->getId())->getToken();
- return $tokenKey;
- }
- }
- private function getCustomerToken($customerId)
- {
- $customerToken = $this->tokenModelFactory->create();
- $tokenKey = $customerToken->createCustomerToken($customerId)->getToken();
- return $tokenKey;
- }
- }
Add Comment
Please, Sign In to add comment