5

I am creating an app for Magento 2 but there is no any API for a customer to check if user/customer is authenticated or not. Followed : http://devdocs.magento.com/swagger/index.html#/

But there is not any solution.

1
  • I have tried by that way @Randall Anthony Bondoc, but its not working for me with postman, can you please explain me in detail how i can use customer login authentication with postman with inbuilt api? Commented Aug 27, 2018 at 14:35

3 Answers 3

6

You should use this API:
integrationCustomerTokenServiceV1:

Interface providing token generation for Customers
POST /V1/integration/customer/toke

http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html

4

you can use the authenticate function in accountmanagerinterface. in your webapi.xml file include this code.

<route url="/V1/customers/authenticate/:email/:password" method="GET">
    <service class="Magento\Customer\Api\AccountManagementInterface" method="authenticate"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

you can call this via curl in php.

    $userData = array("username" => "your_username", "password" => "your_password");
    $ch = curl_init("http://localhost/magento/index.php/rest/V1/integration/admin/token");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));

    $token = curl_exec($ch); 

    $ch = curl_init("http://localhost/magento/index.php/rest/V1/customers/authenticate/".
        "[email protected]". //username/email
        "/".
        "[email protected]" //password
        );
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));

    $result = curl_exec($ch);

    echo($result);
3
  • 1
    this code working for me thank you so much.
    – kunj
    Commented Jul 14, 2018 at 9:43
  • Hi @Randall i am getting below error {"message":"Request does not match any route.","trace":null} what to do now to solve this error. Please suggest me. Commented Oct 26, 2018 at 12:41
  • hello i am also getting error Request does not match any route. can you help me regarding this Commented Mar 13, 2019 at 7:50
0

For creating an App in MAgento 2, you can follow the REST API for a customer to authenticate.

For Customer Token: Magento grants access to resources with the anonymous or self permission.

Use the API: Endpoint: POST /rest/<store_code>/V1/integration/customer/token

Soap: integrationCustomerTokenServiceV1

For full details please check: https://www.cloudways.com/blog/magento-2-rest-api/

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.