Post by dzulu » Fri Mar 01, 2024 7:19 pm

Hello everyone,

I'm currently trying to update my plugin from Opencart 3 version to 4. The goal is to log a user but I can't. I'm struggling with sessions and I'm not sure why. When I'm trying to show session data, the session id is all the time changing and so data too.
For example, in a custom function I'm doing this :

Code: Select all

public function myfunction(){
....
$this->session->data['test'] = 'ok';
$this->response->redirect($this->url->link('extension/myplugin/module/plugin.test', '', true));
}
                                           
public function test(){        
   var_dump($this->session->getId());
   var_dump($this->session->data);
   die('ok');
}
The only data that are into session data is the currency and the session ID changes all the time.

Nevertheless, if I'm doing another test accessing this url mylocalsite.loc/index.php?route=extension/myplugin/module/plugin.test1 :

Code: Select all

public function test1(){
   $this->session->data['test'] = 'ok';
   $this->response->redirect($this->url->link('extension/myplugin/module/plugin.test', '', true));
}    
public function test(){        
   var_dump($this->session->getId());
   var_dump($this->session->data);
   die('ok');
}
This time the session data contain the "test" index and id is still the same. Am I doing something wrong ? The problem is that I'm login my user using customer default function login that return true and I'm getting all session data set correctly (checking using XDebug), but on the next page (account/account), session data are lost and so user is logout.

Thank you.

Newbie

Posts

Joined
Fri Oct 06, 2017 9:48 pm

Post by ADD Creative » Fri Mar 01, 2024 11:12 pm

How are you triggering/calling your custom function?

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Sat Mar 02, 2024 10:12 am

dzulu wrote:
Fri Mar 01, 2024 7:19 pm
Hello everyone,

I'm currently trying to update my plugin from Opencart 3 version to 4. The goal is to log a user but I can't. I'm struggling with sessions and I'm not sure why. When I'm trying to show session data, the session id is all the time changing and so data too.
For example, in a custom function I'm doing this :

Code: Select all

public function myfunction(){
....
$this->session->data['test'] = 'ok';
$this->response->redirect($this->url->link('extension/myplugin/module/plugin.test', '', true));
}
                                           
public function test(){        
   var_dump($this->session->getId());
   var_dump($this->session->data);
   die('ok');
}
The only data that are into session data is the currency and the session ID changes all the time.

Nevertheless, if I'm doing another test accessing this url mylocalsite.loc/index.php?route=extension/myplugin/module/plugin.test1 :

Code: Select all

public function test1(){
   $this->session->data['test'] = 'ok';
   $this->response->redirect($this->url->link('extension/myplugin/module/plugin.test', '', true));
}    
public function test(){        
   var_dump($this->session->getId());
   var_dump($this->session->data);
   die('ok');
}
This time the session data contain the "test" index and id is still the same. Am I doing something wrong ? The problem is that I'm login my user using customer default function login that return true and I'm getting all session data set correctly (checking using XDebug), but on the next page (account/account), session data are lost and so user is logout.

Thank you.
OC 4 version.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by dzulu » Tue Mar 05, 2024 7:17 pm

ADD Creative wrote:
Fri Mar 01, 2024 11:12 pm
How are you triggering/calling your custom function?
Hello,

For this test, I only tried by taping the url into the browser to check session but in "normal" mode, the module should redirect user from the OC site to my API and then redirect to the OC site. The API redirect the user on the url :

Code: Select all

rtrim($base_uri, ' /') . '/index.php?route=extension/mymodule/module/mymodule.callback';


When coming back to the OC site into callback function, I force the user login (if the API result is successful) :

Code: Select all

$this->customer->login($result['email'], '', true)
Then redirect the user via (I tried with and without url parameters too) :

Code: Select all

$this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') .(isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''), true));


But when I'm coming to the page account/account, if I check session data, ie customer_id (as the user should be logged), it is empty. I tried to set custom session data but they are also lost on the next page too as the session is not the same.
Plugin is fully working with the same behavior (session etc..) on Opencart 3.x.

I'm testing on Opencart 4.0.23.

Thank you.

Newbie

Posts

Joined
Fri Oct 06, 2017 9:48 pm

Post by ADD Creative » Tue Mar 05, 2024 8:00 pm

Check the cookies in your web browser developer tool. You may find you have multiple cookies for different subdomains or directories.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Wed Mar 06, 2024 7:35 am

dzulu wrote:
Tue Mar 05, 2024 7:17 pm
ADD Creative wrote:
Fri Mar 01, 2024 11:12 pm
How are you triggering/calling your custom function?
Hello,

For this test, I only tried by taping the url into the browser to check session but in "normal" mode, the module should redirect user from the OC site to my API and then redirect to the OC site. The API redirect the user on the url :

Code: Select all

rtrim($base_uri, ' /') . '/index.php?route=extension/mymodule/module/mymodule.callback';


When coming back to the OC site into callback function, I force the user login (if the API result is successful) :

Code: Select all

$this->customer->login($result['email'], '', true)
Then redirect the user via (I tried with and without url parameters too) :

Code: Select all

$this->response->redirect($this->url->link('account/account', 'language=' . $this->config->get('config_language') .(isset($this->session->data['customer_token']) ? '&customer_token=' . $this->session->data['customer_token'] : ''), true));


But when I'm coming to the page account/account, if I check session data, ie customer_id (as the user should be logged), it is empty. I tried to set custom session data but they are also lost on the next page too as the session is not the same.
Plugin is fully working with the same behavior (session etc..) on Opencart 3.x.

I'm testing on Opencart 4.0.23.

Thank you.
The provided code on the above can also be tested in the event/debug controller to see if you are successfully able to handle the session data. It should work from this location to test this out.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by dzulu » Mon Mar 11, 2024 11:19 pm

Hello everyone and thank you for all of your answers.

So, I've made some new tests on the platform. What is strange is when I'm checking the cookie using the developper tool, I got all the time twice cookies (they are the same) for the same domain but data are the same (session etc...).

So checking for this double cookies domain, I made some changes on the platform and I've found something. If I set the Session Samesite Cookie option to "Lax", everything is working fine. I can register and log myself but I turn it back to "Strict", I could not log anymore (registration is still working but I don't know why because code is pretty the same). Do you have any ideas on how to manage with this strict value as I redirect the user to my API then redirect him back to OP site ? Just for information, I'm testing on HTTP environment.

Thank you

Newbie

Posts

Joined
Fri Oct 06, 2017 9:48 pm

Post by ADD Creative » Tue Mar 12, 2024 12:15 am

If your API is on a different domain then strict will prevent the cookie being sent to the other domain. Hence why you get logged out.

If you are going to redirect to different domains you need to use Lax.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by dzulu » Fri Mar 15, 2024 9:21 pm

Thank you for all your answer. Forcing the lax cookie option is working nicely.

Newbie

Posts

Joined
Fri Oct 06, 2017 9:48 pm
Who is online

Users browsing this forum: No registered users and 1 guest