I've been struggling with the following scenario: i have a custom API endpoint to process the guest to customer login. This endpoint basically does the same thing as /integration/customer/token
with one exception. During the authentication it merges any products the visitor had as a guest into the customer cart of the account they are logging in to.
Problem is that any which way i try, i keep running into the same error: "Invalid state change requested"
when i try to save the updated customer quote.
Some investigation led me to the class \Quote\Model\QuoteRepository\Plugin\AccessChangeQuoteControl
in the Magento_Quote module. This class is a plugin that is triggered before the save()
method of the QuoteRepository. Turns out that as a guest you are not allowed to save a quote that has customer id. And during the authentication procedure you apparently are still a guest, even after authenticating.
Is there any way to make the backend system aware of the fact that the customer is logged in properly without having to do a second API call that contains the Authorization header with the new token? Somehow i have to make sure $this->userContext->getUserType()
in the AccessChangeQuoteControl
plugin returns UserContextInterface::USER_TYPE_CUSTOMER
in stead of UserContextInterface::USER_TYPE_GUEST
but i have no clue on how to make this happen.
Thanks!
Best Answer
Magento gets the customer data with passing anything except token value in API call.
In between magento calls some API related controllers for the same
in this API call magento get customer data based on token.
Based on API url
url="/V1/customers/me"
magento calls function fromMagento calls In-between call of dispatch function.
And this
resolve()
function callsoverride()
functionand
overide()
function callsgetOverriddenValue()
functiongetOverriddenValue()
callsParamOverriderInterface
".../me/..."
route, the route should use only user information specific to the"/customers/me/addresses"
route, the service method"getAddresses($customerId)"
, but in thewebapi.xml
, the $customerId parameterand for business logic of this function is in file -
So this is how Magento get Customer Id between API calls !!
solution 1
solution 2