Login customer via Webservice – Prestashop
I wrote an article a few days ago, in which using the webservice of Prestashop , how to use JSON in post method and put method.
Prestashop Webservice In Post/Put JSON Data In the body
Now in this article, I am going to learn you how to login customer using webservice. what actually happens is that whenever we filter a customer with email or use ?display=full, then we get all the details of the customer but the password is not in the encrypted form that we can use it. And because of this we are not able to complete the login process but don’t worry. After this article, your problem will be solved. Some basic process has to be followed, after that your work will be done. So let’s start_
1. PHP client for PrestaShop Webservices
You have to create this file in the root directory and save the file as PSWebServiceLibrary.php
To download the file this file
The latest version of the PSWebServiceLibrary.php file click here to download the row file:
https://raw.github.com/PrestaShop/PrestaShop-webservice-lib/master/PSWebServiceLibrary.php
After downloading the file, you will have to modify this file a bit. For that, you have to find a function in the PSWebServiceLibrary.php file.
//Find this function called " parseXML"
protected function parseXML($response)
{
}
Actually, this function which will be the response of API returns it in the simple format sure we need in out JSON. So we will comment out the rest of the code blocks and get the response back. Like this:
/**
* Load XML from string. Can throw exception
*
* @param string $response String from a CURL response
*
* @return SimpleXMLElement status_code, response
* @throws PrestaShopWebserviceException
*/
protected function parseXML($response)
{
return $response;
// if ($response != '') {
// libxml_clear_errors();
// libxml_use_internal_errors(true);
// $xml = simplexml_load_string(trim($response), 'SimpleXMLElement', LIBXML_NOCDATA);
// if (libxml_get_errors()) {
// $msg = var_export(libxml_get_errors(), true);
// libxml_clear_errors();
// throw new PrestaShopWebserviceException('HTTP XML response is not parsable: ' . $msg);
// }
// return $xml;
// } else {
// throw new PrestaShopWebserviceException('HTTP response is empty');
// }
}
2. Check Valid Customer
Now you have to create a new file in the root itself, whose name you can give whatever you want, according to yourself, I create a file named login.php in my case.
In this file we will include client webservices which have to pass three parameters.
// get information from PrestaShop
$webService = new PrestaShopWebservice($url, $key, $debug);
$url: Pass the URL of Prestashop shop domain without “/api”
$key: Pass the customer api key
$debug : false ;
Now a data set has to be created to get the data from the API, in which the name of the API will be if you want to do some filters, then that will also happen, here we are filtering the customer by email, then we will give the email and in the last response to you. What field is required to display? Like:
$optUser = array(
'resource' => 'customers',
'filter[email]' => '[' . $email . ']',
'display' => '[id,email,lastname,firstname,passwd]'
);
Now we have both the password that the customer has given with the email and the password received from the table, now we can easily verify the password using password_verify(). we can accept $input_password when this returns true, otherwise $input_password is invalid.
$pass_check = password_verify($password, $application_password);
if (password_verify($password, $application_password) == true) {
session_start();
$response = array();
$response['status'] = 'succes';
$response['body'] = $resultUser;
setcookie("userId", $info->id);
header('Content-type: application/json');
echo json_encode($response);
} else {
$response = array();
$response['status'] = 'error';
$response['message'] = 'Wrong password';
header('Content-type: application/json');
echo json_encode($response);
}
Full Code :
<?php
if (empty($_REQUEST['email']) || empty($_REQUEST['password']) && empty($_REQUEST['key'])) {
header('HTTP/1.1 404 Not Found'); //This may be put inside err.php
exit(0);
}
require_once('PSWebServiceLibrary.php');
$base_url="https://".$_SERVER['SERVER_NAME'].dirname($_SERVER["REQUEST_URI"].'?');
$url = $base_url; // PS shop domain without "/api"
$key = $_REQUEST['key'];
$debug = false; //true;
// get information from PrestaShop
$webService = new PrestaShopWebservice($url, $key, $debug);
$email = $_REQUEST['email']; // $_REQUEST['email'];
$password = $_REQUEST['password'];
$optUser = array(
'resource' => 'customers',
'filter[email]' => '[' . $email . ']',
'display' => '[id,email,lastname,firstname,passwd]'
);
$resultUser = json_decode(($webService->get($optUser)));
$application_password = $resultUser->customers[0]->passwd;
$pass_check = password_verify($password, $application_password);
if (password_verify($password, $application_password) == true) {
session_start();
$response = array();
$response['status'] = 'succes';
$response['body'] = $resultUser;
setcookie("userId", $info->id);
header('Content-type: application/json');
echo json_encode($response);
} else {
$response = array();
$response['status'] = 'error';
$response['message'] = 'Wrong password';
header('Content-type: application/json');
echo json_encode($response);
}
?>
To call the API like this:
https://yourdomain.com/prestashop_directory/login.php?email=customer.email@abc.com&password=password&key=32ZPJ4PQAZ9HF1QZPTVRT7T29MG6XJAZ
Response
