Jump to content

Custom Registration Issue


jayashanka

Recommended Posts

I'm trying to register a customer from backend and after successful registration login the customer automatically and redirect to my-account page. 

registration works well, but when I try to login the customer automatically and redirect to my-account, it redirects back to login page and ask to login.

below is my code

$customer = new Customer();
                        $authentication = $customer->getByEmail($email);
                        if (Validate::isLoadedObject($authentication)) {
     
                            $this->context->cookie->id_customer = $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 = $this->context->customer->isGuest();
                            $this->context->cookie->passwd = $this->context->customer->passwd;
                            $this->context->cookie->email = $this->context->customer->email;
                 
                            Tools::redirect('index.php?controller=my-account');
                        }

 

Is there anything wrong with my code?

 

 

 

Link to comment
Share on other sites

Hi,

Before redirecting to the "my-account" page, you should make sure that the customer is successfully logged in. You can do this by verifying the "logged" status in the cookie.

if ($this->context->cookie->logged == 1) {
  Tools::redirect('index.php?controller=my-account');
}

Make sure that the "my-account" controller and template are correctly configured in your PrestaShop installation. It's possible that there may be an issue with the controller or template that's causing the redirection to the login page.

Thanks!
 

Link to comment
Share on other sites

Also, Validate::isLoadedObject($authentication) simply checks if an object is loaded, but it doesn't verify the customer's password. Using this condition alone would allow any email address to log in without verifying the password.

Instead use something like below :

$customer = new Customer();
$authentication = $customer->getByEmail($email);

if ($authentication && $customer->checkPassword($password)) {
    // Authentication successful
    // Set cookies and redirect to the "my-account" page
} else {
    // Authentication failed, handle the error (e.g., show an error message)
}

Let me know if it helps!

Thanks!

  • Thanks 1
Link to comment
Share on other sites

On 8/21/2023 at 11:33 AM, AddWeb Solution said:

Also, Validate::isLoadedObject($authentication) simply checks if an object is loaded, but it doesn't verify the customer's password. Using this condition alone would allow any email address to log in without verifying the password.

Instead use something like below :

$customer = new Customer();
$authentication = $customer->getByEmail($email);

if ($authentication && $customer->checkPassword($password)) {
    // Authentication successful
    // Set cookies and redirect to the "my-account" page
} else {
    // Authentication failed, handle the error (e.g., show an error message)
}

Let me know if it helps!

Thanks!

Hi @AddWeb Solution thanks for sharing this code. its really helpful. And also I have done additional part that is execuiting Authentication hook.

with both combination, functionality works well. 

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