简体   繁体   中英

Customer not login programmatically in magento

I am trying to login the customer programmatically in magento. I have used curl and develop my soap api in magento.

Requirement: I have two website one in laravel and other in magento and all the email and password are setup on both website. When user login into laravel website then customer must login into magento website or vice versa. Single sign on concept.

Using Soap Api, passing parameter email and password to my soap client. Below is code:

class Axovel_Customlogin_Model_Custom_Api extends Mage_Api_Model_Resource_Abstract
 {
        public function login($email,$password) {
              $login_customer_result = Mage::getModel('customer/customer')->setWebsiteId(1)->authenticate($email, $password);

            if ($login_customer_result == 1) {

                Mage::getSingleton("core/session", array("name" => "frontend"));

                $websiteId = Mage::app()->getWebsite()->getId();
                $store = Mage::app()->getStore();
                $customer = Mage::getModel("customer/customer");
                $customer->website_id = $websiteId;
                $customer->setStore($store);
                try {
                    $customer->loadByEmail($email);
                    //$session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
                   $session = Mage::getSingleton('customer/session');
                    $session->login($email, $password);
                    $session->setCustomerAsLoggedIn($session->getCustomer());

                    return $session->getSessionId();
                }catch(Exception $e){

                    echo $e;

                }
    }
    }
 }

By calling this method through soap call it will return the session id and also authenticate the customer but not login the customer on frontend.

I have tried the curl to call the loginPost action of customer module and pass the form key, below is curl code:

$string = "login[username]=xyz@mymail.com&login[password]=123456&form_key=B3pr9GSMp75kwW20";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/performer/index.php/customer/account/loginPost');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.session_id());
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_HEADER, true);

$output = (string) curl_exec($ch);

This will also works without any error but customer is not login.

I have create the html form on magento root using action

http://127.0.0.1/performer/index.php/customer/account/loginPost

and text field are email, password and formkey and on submit the form it will successfully logged into magento.

But not able to login using api and curl.

Try $session-> loginById and $session-> setCustomerAsLoggedIn :

Lifted from a past project of mine:

$session = Mage::getSingleton( 'customer/session' );
try
{
    $session->loginById( $customer->getId());
    $session->setCustomerAsLoggedIn( $session->getCustomer() );
}
catch( Exception $e )
{
    return false;
}
Mage::app()->getResponse()->setRedirect(Mage::getUrl('customer/account/index'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM