Question

  • I want to login customer from external Laravel App to magento site.
  • I have multi store multi website setup (mean every store have separate website)

I try all available ways:

1) By importing Magento OM in Laravel app & try to login like its successfully authenticating the customer but on refreshing the Magento page customer not log in:

$store = $this->_objManager->create('Magento\Store\Model\Store')->load($storeId);            
// Load customer
$customer = $this->_objManager->create('Magento\Customer\Model\Customer')->setStore($store);
if($customer->authenticate($email, $password)){
    $customerSession = $this->_objManager->create('Magento\Customer\Model\Session');
    $customerSession->loginById($customer->getId());
    $customerSession->regenerateId();

    if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
        $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
        $metadata->setPath('/');
        $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
    }

    if($customerSession->isLoggedIn()) {
        echo $customer->getId(). "----Customer Logged in";
    }else{
        echo "customer is Not Logged in";
    }
}

In above case in Laravel controller printing: 145----Customer Logged in mean code wise I am getting customer session created properly in Laravel App. But on Magento site page reload customer not login.

2) I Also Try LoginPost Method:

I post ajax request from Laravel form

    xhttp.open("POST", "http://ie.domainName.com/customer/account/loginPost/", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.setRequestHeader("Content-length", params.length);
        xhttp.setRequestHeader("Connection", "close");
        xhttp.send(params);

to this controller same its authenticating the customer and & I get customers id in the execute function after authentication. But here as well when I try to reload Magento page in same browser its not login customer. What I am missing here ?

Was it helpful?

Solution 3

Well The issue is multi site cookies.

In M1 above solution will work if your code base present on same server no issue in it, It will work for multi websites. It will create cookie for all websites & for all stores.

In M2 its not creating cookie for other then base website when external request received. It will create cookie for base website. So when you try to login from other website it don't generate PHPSESSION cookies as per authorized used.

So solution is Move Laravel repository/folder into the Magento Base directory, now it will work like a charm for multi website environment.

OTHER TIPS

Create plugins in this interface: Magento\Customer\Api\AccountManagementInterface. It will handle all the cookies/sessions, no need to do this yourself. e.g. to authenticate you could do something like this:

public function aroundAuthenticate(
    \Magento\Customer\Api\AccountManagementInterface $subject,
    callable $proceed,
    string $email,
    string $password
): CustomerInterface {
    $customer = /* Login logic here */
    $customerModel = $this->customerFactory->create()->updateData($customer);

    $this->eventManager->dispatch(
        'customer_customer_authenticated',
        ['model' => $customerModel, 'password' => $password]
    );

    $this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);

    return $customer;
}

Hasan, you can use magento api to get user authentication from magento, rather then creating any plugin.

$wsdl = 'http://testwebsite.com/index.php/rest/V1/'; $apiUser = 'testuser'; $apiKey = 'testPass';

to get token of API call this API (+ mean concatenate the api path with wsdl) $wsdl + integration/admin/token with json parameters of

"username" => $apiUser, "password" => $apiKey

To get authentication of user login credentials

API = $wsdl + integration/customer/token

API Call { "username":"test@test.comm", "password":"TEst123!" } Let me know if you need further clarifiction.

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top