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

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();

      }
?>
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.

4
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;
    }
}

Your Answer

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

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