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


to consume magento rest api web services.

We get tokens and return customer details successfully form that resource


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){

    $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

  • You can get the customer id from "/rest/V1/customers/me" this API. – manish_khot Aug 17 '17 at 8:38
  • You need customer Id in other call after login for self resource ? – Pankaj Pareek Aug 23 '17 at 8:46

Try Below Code To get a customer id

    $obm = \Magento\Framework\App\ObjectManager::getInstance();
    $context = $obm->get('Magento\Framework\App\Http\Context');

    $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
      $om = \Magento\Framework\App\ObjectManager::getInstance();
      $customerSession = $om->create('Magento\Customer\Model\Session');
         //get a customer id       
      $customerId = $customerSession->getCustomer()->getId();

  • Didn't work at all still consider the user guest and customerId is NULL. – Mohammed Gomma Aug 19 '17 at 12:12

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.