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
-
You can get the customer id from "/rest/V1/customers/me" this API.manish_khot– manish_khot2017年08月17日 08:38:26 +00:00Commented Aug 17, 2017 at 8:38
-
You need customer Id in other call after login for self resource ?Pankaj Pareek– Pankaj Pareek2017年08月23日 08:46:19 +00:00Commented Aug 23, 2017 at 8:46
3 Answers 3
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();
}
?>
-
Didn't work at all still consider the user guest and customerId is NULL.Mohammed Gomma– Mohammed Gomma2017年08月19日 12:12:12 +00:00Commented Aug 19, 2017 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.
-
hi @mohammed i would like to know your question is related to REST Apis? thanksNagaraju Kasa– Nagaraju Kasa2017年11月02日 09:37:26 +00:00Commented Nov 2, 2017 at 9:37
-
- Yes, it is ..Mohammed Gomma– Mohammed Gomma2017年11月02日 10:12:56 +00:00Commented Nov 2, 2017 at 10:12
-
thanks for the replay mohammed could u plz look into my question ? magento.stackexchange.com/questions/199772/…Nagaraju Kasa– Nagaraju Kasa2017年11月02日 10:14:32 +00:00Commented Nov 2, 2017 at 10:14
-
whether have any idea on this api?Nagaraju Kasa– Nagaraju Kasa2017年11月02日 11:13:25 +00:00Commented Nov 2, 2017 at 11:13
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;
}
}