I am using rest API to login in customer account usig the below URL and details:
URL: https://magento.host/index.php/rest/V1/integration/customer/token
Header: "Content-Type:application/json"
Data: "{"username":"[email protected]", "password":"customer1pw"}
and successful request returns a response body with the token:
asdf3hjklp5iuytre
As we are now logged in and all the call after login that we need to make using Rest API should use the below format:
URL: http://magento.ll/index.php/rest/V1/customers/me
Header: Authorization: Bearer asdf3hjklp5iuytre
Now My Question is:
How we will verify that token is valid for other subsequent API call ? How can we get customer details using Token Provided in Header ?
Please explain the flow.
Updated:
When we try to access this below API url with valid Headers, It call getById
function of CustomerRepository
. But in getById
function there is one argument called $customerId
. I am not sure how it is coming but In cumstomer module webapi.xml
for this function, I found there is way to pass customerId
as param and It will not send from requester but Magento set it Internally.
http://magento.ll/index.php/rest/V1/customers/me
vendor/magento/module-customer/etc/webapi.xml
<route url="/V1/customers/me" method="GET">
<service class="Magento\Customer\Api\CustomerRepositoryInterface" method="getById"/>
<resources>
<resource ref="self"/>
</resources>
<data>
<parameter name="customerId" force="true">%customer_id%</parameter>
</data>
So I did the same thing, In my function and It is working fine. Here my doubt: Do we don't need to check if token is valid or not ? Is this all managed by Magento Internally ?