Jump to content

How to log in a user from an external page?


Recommended Posts

I need to log in the user from my page and to control where the user will be redirected. The problem with the first suggestion was that when I send a POST request to the authetication.php page with params as described, it doesn't log in the user, but load the login page with already filled in username and password. May be it needs some more params to be more cooperative :).

Thank you for your help!

Link to comment
Share on other sites

I figured this out! I ended up with this php function:

   function psLogin($email, $password) {
       global $cookie;
       $customer = new Customer();
       $authentication = $customer->getByEmail(trim($email), trim($password));
       if ( $authentication and $customer->id ) {
           $cookie->logged = 1;
           $cookie->id_customer = intval($customer->id);
           $cookie->customer_lastname = $customer->lastname;
           $cookie->customer_firstname = $customer->firstname;
           $cookie->passwd = $customer->passwd;
           $cookie->email = $customer->email;
           Module::hookExec('authentication');
           return true;
       } else {
           return false;
       }
   }



It works exactly as I needed :).

Link to comment
Share on other sites

  • 1 month later...

Excuse me if my question seems stupid, but could you give me a small example or place where i can look on how to create a handler for PrestaShop? I would like to create a form and submit the username/password and if succesfull redirect to another page.

Link to comment
Share on other sites

  • 2 years later...
  • 3 months later...

I figured this out! I ended up with this php function:

 

function psLogin($email, $password) {        global $cookie;        $customer = new Customer();        $authentication = $customer->getByEmail(trim($email), trim($password));        if ( $authentication and $customer->id ) {            $cookie->logged = 1;            $cookie->id_customer = intval($customer->id);            $cookie->customer_lastname = $customer->lastname;            $cookie->customer_firstname = $customer->firstname;            $cookie->passwd = $customer->passwd;            $cookie->email = $customer->email;            Module::hookExec('authentication');            return true;        } else {            return false;        }    }

It works exactly as I needed :).

 

 

I have a similar problem. I see that the post is marked solved. However, I do not understand where to implement the code. Sorry if I am being too dumb. I am actually a newbie in it.

Link to comment
Share on other sites

  • 7 years later...

Here is solution that works for me in 1.7

 

$authentication =  $customer->getByEmail($email, $password);
$authentication->logged = 1;
$this->context->customer = $authentication;
$this->context->cookie->id_customer = (int) $authentication->id;
$this->context->cookie->customer_lastname = $authentication->lastname;
$this->context->cookie->customer_firstname = $authentication->firstname;
$this->context->cookie->logged = 1;
$this->context->cookie->check_cgv = 1;
$this->context->cookie->is_guest = false;
$this->context->cookie->passwd = $authentication->passwd;
$this->context->cookie->email = $authentication->email;

$this->context->cookie->registerSession(new CustomerSession());

 

Link to comment
Share on other sites

  • 4 weeks later...

Hi everyone!

I figured I could share a piece of code. It might help one or the other 😀

I'm curretly working on the major 1.7.7.

/**
 * Updates the customer in the context of the shop
 *
 * @param Customer $customer
 * @return bool
 */
private function loginCustomer(Customer $customer)
{
    try {
        // update customer does all the work
        $this->context->updateCustomer($customer);
    } catch (Exception | PrestaShopException $e) {
        return false;
    }
    return true;
}

And a simple authenticate use case could look like this:

/**
 * Tries to authenticate a customer with the given email address
 *
 * @param string $mail A valid email address
 * @return bool Returns true is authenticated
 */
private function authenticateCustomer($mail)
{
    try {
        $customer = '';
        // check whether a customer with the given email address already exists
        if (!Customer::customerExists($mail)) {
            // if the customer doesn't exist -> create
            // you can use your own create function with your preferred way. It should return a Customer object
            $customer = $this->createCustomer();
        } else {
            // or if a user already exists
            // get the existing customer object by mail
            $customer = (new Customer())->getByEmail(trim($mail));
        }
        // finally login the customer
        $this->loginCustomer($customer);
    } catch (Exception | PrestaShopException $e) {
        return false;
    }
    return true;
}

I hope this helps.

 

Cheers!

  • Like 1
Link to comment
Share on other sites

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...