Jump to content

Prestashop Custom Login Not Working [SOLVED]


Recommended Posts

I am working on a custom Prestashop module that enables user login via a token passed in the URL. Despite successfully retrieving the token, validating it, and updating the customer's session with Context::cookie, the login process is not reflecting in the Prestashop fronted.
 

<?php



if (!defined('_PS_VERSION_')) {

exit;

}



class tokenlogintokenloginModuleFrontController extends ModuleFrontController

{

public function initContent()

{

parent::initContent();



// Get the token from the URL

$token = Tools::getValue('token');

if (!$token) {

error_log('Token not provided!');

return; // Handle error appropriately

}



// Validate the token

$customerId = $this->validateToken($token);

if (!$customerId) {

error_log('Invalid or expired token: ' . $token);

return; // Handle error appropriately

}



// Log in the user

$this->loginCustomer($customerId);

// echo "<pre>";print_r($this->context->cookie); ;exit;



// Check if the user is logged in

if ($this->context->cookie->logged) {

// Set customer data

$customerName = $this->context->cookie->customer_firstname . ' ' . $this->context->cookie->customer_lastname;

// Assign these variables to the context

$this->context->smarty->assign([

'logged' => true,

'customerName' => $customerName,

'urls' => [

'actions' => [

'logout' => $this->context->link->getPageLink('my-account', null, null, ['logout' => true]),

],

'pages' => [

'my_account' => $this->context->link->getPageLink('my-account'),

'authentication' => $this->context->link->getPageLink('authentication'),

],

'current_url' => Tools::getHttpHost(true) . $_SERVER['REQUEST_URI'],

],

]);

} else {

// If not logged in, assign logged status as false

$this->context->smarty->assign('logged', false);

}

Tools::redirect($this->context->link->getPageLink('my-account', true));



}



private function validateToken($token)

{

// Fetch the customer ID by matching the secure key

$sql = 'SELECT `id_customer` FROM `' . _DB_PREFIX_ . 'customer` WHERE `secure_key` = "' . pSQL($token) . '"';

return Db::getInstance()->getValue($sql) ?: false;

}



// http://localhost/prestashop/module/tokenlogin/tokenlogin?token=7cb1cf6919827474a03871aff6ea975b

private function loginCustomer($customerId)

{

global $context;



$customer = new Customer($customerId);

if (Validate::isLoadedObject($customer)) {

// Set customer data

$this->context->customer = $customer;

$this->context->cookie->id_customer = (int)$customer->id;

$this->context->cookie->customer_lastname = $customer->lastname;

$this->context->cookie->customer_firstname = $customer->firstname;

$this->context->cookie->passwd = $customer->passwd;

$this->context->cookie->logged = 1;

$this->context->cookie->email = $customer->email;

$this->context->cookie->is_guest = 0; // Not a guest

$this->context->cart->secure_key = $customer->secure_key;



// Persist the cookie

$this->context->cookie->write();



// Fire hooks for customer authentication

Hook::exec('actionAuthentication', ['customer' => $this->context->customer]);



// Check and handle cart rules

CartRule::autoRemoveFromCart($this->context);

CartRule::autoAddToCart($this->context);



error_log('Customer logged in with ID: ' . $customerId);

} else {

error_log('Invalid customer ID: ' . $customerId);

}

}



private function updateCart($customer)

{

// Ensure the cart is updated for the logged-in customer

if (!$this->context->cart->id || $this->context->cart->id_customer != $customer->id) {

$this->context->cart->id_customer = (int)$customer->id;

$this->context->cart->id_address_delivery = $this->context->cart->id_address_delivery ?: 0;

$this->context->cart->id_address_invoice = $this->context->cart->id_address_invoice ?: 0;

$this->context->cart->id_lang = $this->context->language->id;

$this->context->cart->id_currency = $this->context->currency->id;

$this->context->cart->secure_key = $customer->secure_key;

$this->context->cart->save();

$this->context->cookie->id_cart = (int)$this->context->cart->id;

}

}



}

the context shows customer as logged in 

but in the home page header user information not showing 

Please help me here

Edited by prasoonh (see edit history)
Link to comment
Share on other sites

Try to use

 $customer = new Customer($id_customer);
 if (Validate::isLoadedObject($customer) && $customer->id) {
    $this->context->updateCustomer($customer);
    Tools::redirect('index.php?controller=my-account');
 }
 else {	// Not a valid user or deleted
   Tools::redirect('index.php');
 }

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • prasoonh changed the title to Prestashop Custom Login Not Working [SOLVED]

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