1

We are using token based authentication over rest api from that resource

/rest/V1/integration/customer/token?username=USER_NAME&password=P@$$WORD

to consume magento rest api web services.

We get tokens and return customer details successfully form that resource

/rest/V1/customers/me

I need to get the customer ID from his session but I can not because user always considered not logged in, despite working fine for web scenario below is the code snippet I use.

 public function __construct(\Magento\Customer\Model\Session $session){
 parent::__construct($context);
 $this->customerSession = $session;
}

I am facing that problem inside my custom offline payment method defined in custom extension, I need to know the customer ID in order to determine if the customer can use that payment method or not.

Even if I tried to use the checkoutSession, still can not get the customerId.

$om = \Magento\Framework\App\ObjectManager::getInstance();
 $checkoutSession = $om->get('\Magento\Checkout\Model\Session');
 exit(var_dump($checkoutSession->getQuote()->getCustomer()->getId())); //return NULL

The custom extension works fine when viewing the store from the browser.

Can you please help me what is wrong here?

Thanks in advance

Vivek Kumar
5,7932 gold badges26 silver badges55 bronze badges
asked Aug 14, 2017 at 15:08
2
  • You can get the customer id from "/rest/V1/customers/me" this API. Commented Aug 17, 2017 at 8:38
  • You need customer Id in other call after login for self resource ? Commented Aug 23, 2017 at 8:46

3 Answers 3

2

Try Below Code To get a customer id

<?php
 $obm = \Magento\Framework\App\ObjectManager::getInstance();
 $context = $obm->get('Magento\Framework\App\Http\Context');
 $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
 if($isLoggedIn){ 
 $om = \Magento\Framework\App\ObjectManager::getInstance();
 $customerSession = $om->create('Magento\Customer\Model\Session');
 //get a customer id 
 $customerId = $customerSession->getCustomer()->getId();
 }
?>
answered Aug 19, 2017 at 5:33
1
  • Didn't work at all still consider the user guest and customerId is NULL. Commented Aug 19, 2017 at 12:12
1

The right answer is that Magento didn't load the customer session as used in web session based authentications, So I have to send customer email or id and load the customer model.

answered Aug 21, 2017 at 15:01
4
  • hi @mohammed i would like to know your question is related to REST Apis? thanks Commented Nov 2, 2017 at 9:37
  • - Yes, it is .. Commented Nov 2, 2017 at 10:12
  • thanks for the replay mohammed could u plz look into my question ? magento.stackexchange.com/questions/199772/… Commented Nov 2, 2017 at 10:14
  • whether have any idea on this api? Commented Nov 2, 2017 at 11:13
0

For everyone who is coming to this question, and hasn't got yet a solution. I have faced the same problem. My task was to validate if the customer was logged in on the VUE JS Storefront. Rest API for creating customer token doesn't create Session object for logged in customer.

The solution, in this case, is, to get the customer token and load by token

<?php
use Magento\Integration\Model\Oauth\TokenFactory;
class Example
{
 /**
 * @var TokenFactory
 */
 private $tokenFactory;
 /**
 * Example constructor.
 * @param TokenFactory $tokenFactory
 */
 public function __construct(
 TokenFactory $tokenFactory
 ) {
 $this->tokenFactory = $tokenFactory;
 }
 /**
 * @param string $token
 * @return bool|\Magento\Integration\Model\Oauth\Token
 */
 public function getTokenData($token)
 {
 $tokenFactory = $this->tokenFactory->create();
 $tokenData = $tokenFactory->loadByToken($token);
 if($tokenData->getToken() !== null) {
 return $tokenData;
 }
 return false;
 }
}
answered Mar 16, 2020 at 15:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.