5

I have a custom module, which basically adds social media login in magento. However I am facing an issue with core/session which I dont know how to resolve.

I am adding my custom blocks in customer login page via my own layout xml file. Every block is loading successfully. One of my cusotm block is setting a variable in core/session through it's _construct() method as like this.

File : app\code\local\Mynamespace/Mymodule/Block/Entity/Someblock.php

<?php
class Mynamespace_Mymodule_Block_Entity_Someblock extends Mage_Core_Block_Template
{
     protected function _construct() {
        parent::_construct();

        // CSRF protection
        $csrf = md5(uniqid(rand(), true));
        Mage::getSingleton('core/session')->setGoogleCsrf($csrf);
        $this->client->setState($csrf);

    }

}

I ensured that, session variable is set when the page loads. Now in the next process/request, I need to validate google_csrf variable that is set in core/session. But the problem is Mage_Core_Model_Session appears to be empty in the next process. Suppose I need to validate session during the request of url www.domain.com/frontname/google/connect, then I checked core/session in the corresponding controller file like this.

File : app\code\local\Mynamespace/Mymodule/controllers/GoogleController.php

public function connectAction()
{
   print_r(Mage::getSingleton('core/session'));die();
}

But it outputs Mage_Core_Model_Session with no variable set in it. Why it happens like this?

More info: actually the block in question is used to show google button in frontend. When click on that button, it will force user to authenticate him with google account. I am checking core/session in the controller that processes the redirect url. I am getting the response successfully. But core/session that set before this action seems empty.

You have any thoughts about it ? Let me know.

2 Answers 2

2

I suspect the controller processing the redirect URL may be getting the core/session model before the customer's session has been re-started.

Mage_Core_Controller_Varien_Action::preDispatch starts the session back up by calling Mage_Core_Model_Session::start (inherited from Mage_Core_Model_Session_Abstract_Varien). This is responsible for starting or resuming a user's session, allowing the various session models to reconnect with their data.

Instantiating a session model before the controller's preDispacth has re-started the session will result in a session model that is empty. This may result from getting the instance in the controller's _construct method, even if it is only actually read from after preDispatch.

1
  • I am checking this variable inside the controller method.For an example, redirect_url looks like www.domain.com/frontname/google/connect. I am checking session inside Mynamespace_Mymodule_GoogleController::connectAction(), which I think processed after preDispatch method. Commented Oct 30, 2014 at 14:19
0

If the request to your controller is done using an ajax call, then it will be a different session, with different session_id();

Try echo your session_id in your block where you set the session variable and in your controller and see if its the same.

1
  • No I am not using any ajax request for this. You have any other thoughts? Thanks for your time. Commented Oct 30, 2014 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.