Jump to content

Auto Login Session Problem


qw3r7y

Recommended Posts

Hi all,
 
the situation is as follows: When I enter my wordpress account automatically log the desire in prestashop too.
 
For this, taking the idea of AuthController I can log in correctly (says Welcome user) but if I go to another menu page, actually there is no logged!
 
I tried two codes that leave them below:
 

with CURL:  

$url =  'http://myshop/authentication';
    
			    // post params of the login form    
			    $post_params = array('email' => '[email protected]',
			                         'passwd' => 'password',
			                         'stay_logged_in' => 1,
			                         'SubmitLogin' => 1);
			    
			    //Create a curl object
			    $ch = curl_init();
			    
			    //Set the useragent
			    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);    
			    //Set the URL
			    curl_setopt($ch, CURLOPT_URL, $url);
			    
			    //This is a POST query
			    curl_setopt($ch, CURLOPT_POST, 1);    
			    //Set the post data
			    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
			    
			    //We want the content after the query
			    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
			    //Follow Location redirects
			    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
				//curl_setopt($ch, CURLOPT_HEADER, 0);
			    
			    /*
			    Set the cookie storing files
			    Cookie files are necessary since we are logging and session data needs to be saved
			    */
			    curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
			    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
			    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
			    
			        
			    //Execute the action to login
			    $post_result = curl_exec($ch);
			    
			    $info = curl_getinfo($ch);
			    curl_close ($ch);
			    
			    echo $post_result;

 

OR: 

$this->pathToPrestashop = '/usr/home/user/public_html';
      include($this->pathToPrestashop.'/config/config.inc.php');
      include_once($this->pathToPrestashop.'/config/settings.inc.php');
      include_once($this->pathToPrestashop.'/classes/Cookie.php');
      include_once($this->pathToPrestashop.'/init.php');
      $cookie = new Cookie('ps');

      //Receive parameters
      //$email=$this->requestData['email'];
      $email='[email protected]';
      $passwd='password';
      /* LOGIN FUNCTION */
      $customer = new Customer();

      $authentication = $customer->getByEmail(trim($email), trim($passwd));
      if (!$authentication) //user doesn't exist
        return false;
      if ( $authentication and $customer->id ) {

Hook::exec('actionBeforeAuthentication');

            $ctx = Context::getContext();
            $ctx->cookie->id_compare = isset($ctx->cookie->id_compare) ? $ctx->cookie->id_compare: CompareProduct::getIdCompareByIdCustomer($customer->id);
            $ctx->cookie->id_customer = (int)($customer->id);
            $ctx->cookie->customer_lastname = $customer->lastname;
            $ctx->cookie->customer_firstname = $customer->firstname;
            $ctx->cookie->logged = 1;
            $customer->logged = 1;
            $ctx->cookie->is_guest = $customer->isGuest();
            $ctx->cookie->passwd = $customer->passwd;
            $ctx->cookie->email = $customer->email;
            // Add customer to the context
            $ctx->customer = $customer;
            //var_dump($ctx->cookie->logged);

            if (Configuration::get('PS_CART_FOLLOWING') && (empty($ctx->cookie->id_cart) || Cart::getNbProducts($ctx->cookie->id_cart) == 0) && $id_cart = (int)Cart::lastNoneOrderedCart($ctx->customer->id)) {
                    $ctx->cart = new Cart($id_cart);
                } else {
                    $id_carrier = (int)$ctx->cart->id_carrier;
                    $ctx->cart->id_carrier = 0;
                    $ctx->cart->setDeliveryOption(null);
                    $ctx->cart->id_address_delivery = (int)Address::getFirstCustomerAddressId((int)($customer->id));
                    $ctx->cart->id_address_invoice = (int)Address::getFirstCustomerAddressId((int)($customer->id));
                }
                $ctx->cart->id_customer = (int)$customer->id;
                $ctx->cart->secure_key = $customer->secure_key;

                if ($this->ajax && isset($id_carrier) && $id_carrier && Configuration::get('PS_ORDER_PROCESS_TYPE')) {
                    $delivery_option = array($ctx->cart->id_address_delivery => $id_carrier.',');
                    $ctx->cart->setDeliveryOption($delivery_option);
                }

                $ctx->cart->save();
                $ctx->cookie->id_cart = (int)$ctx->cart->id;
                $ctx->cookie->write();
                $ctx->cart->autosetProductAddress();

                Hook::exec('actionAuthentication', array('customer' => $ctx->customer));

                // Login information have changed, so we check if the cart rules still apply
                CartRule::autoRemoveFromCart($ctx);
                CartRule::autoAddToCart($ctx);
}

 

 
(email and password assigned, is just to try, then the step as a parameter from WP)
 
If I see the generated txt (cookie.txt), the cookie is perfectly generated, but still fails.
Again, when testing the auto login, it seems I'm logged in, but to change to another page on my site, it is not so!
 
Any idea?
 
Thanks!!!
Edited by qw3r7y (see edit history)
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...