I have a site outside Magento's domain and I should allow Magento customer to login into Magento Store from this external site. Being completely new to Magento , I don't know how to implement this service. I was able to allow registration to Magento from an external site using REST API, but I can't find a similar way to login using REST services.
You can use REST API also. Magento 2 framework builds in already a list of useful API functions.
To make a web API call from a client such as a mobile application, you must supply an authentication token on the call. The token acts like an electronic key that lets you access the API.
So, in your case, for example, we have a login form on your site. We have two input fields: username and password. In your controller, we can get these values and pass to the Magento API. A successful response will return with a token which is used for authentication on the Magento side.
$username = $_POST['email']; $password = $_POST['password'] $userData = array("username" => $username, "password" => $password); $ch = curl_init("http://mage21.loc/rest/V1/integration/customer/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);
I found a solution : - On external site (on sub-domain) i created a cookie with magento token (generated by magento api: /customer/token don't sure about the name of the API) - On magento site (on sub-domain) i created an event-listener on each page except logout page, on this observer i check if there is a token in cookie, if yes i check that this token is present in magento database, if yes i login user
It's work perfectly but you need to be on the same domain or sub-domain for cookies