Getting current customer in custom REST API module
-
12-12-2019 - |
Question
I'm trying to build a series of custom requests for an Android app. Specifically I need to make use of the current customer Id in several of my requests. I've only been using magento for a few weeks, and I'm completely stumped. How can I get the current customer session if the session is stored as a cookie in the browser?
My request is just a join of a few tables based on the customerId, but I can't get a valid id in the first place. Does anyone have any insight on how I can leverage the oauth token to get the related customerId? That's the only idea I've had, but I don't know how to go about it.
Any help would be immensely useful!
Solution
Figured it out! Turns out I need to get the token from the header, and query out the customer id from the Oauth_token table. I'm sure there's a better way for me to retrieve the token from the header, but I couldn't find one in the short term.
$headerString = $this->getRequest()->getHeader('Authorization');
$tokenPosition = strpos($headerString, 'oauth_token=');
$token = null;
if($tokenPosition !== false){
$token = substr($headerString, $tokenPosition+13, 32);
}
if($token == null){
$error = 'Authorization Token Not found.';
throw new Exception($error);
return null;
}
$customerId = Mage::getModel('oauth/token')->load($token,'token')->getData('customer_id');//Mage::getModel('customer/session')->getData();