1

I'm working on a Magento 2 module that may require a lot of form filling in the backend that in some cases may cause the session to expire.
I want to avoid this.
I know that theoretically I can make an ajax call to any page every X seconds to prolong the session, but I want to do it right.
Is there already an action that does nothing else but prolonging the session or do I have to create one?
Or is there an other way to prevent the session from expiring only when on a specific admin page?

6
  • persistent_session_expired event gonna help out?
    – Jackson
    Dec 6, 2016 at 2:06
  • Can't you make you session.gc_maxlifetime higher? Dec 6, 2016 at 8:24
  • I plan to distribute this extension and I cannot control every environment where this is installed. I want to avoid people complaining that they spent 20 minutes filling in a form just to have the session expired.
    – Marius
    Dec 6, 2016 at 8:25
  • 1
    I have no idea what prestashop does (my ecommerce is the only true ecommerce :D ). But this solution seams simple and easy to implement anywhere it is needed.
    – Marius
    Dec 6, 2016 at 8:31
  • 1
    Shots were fired ^^ Dec 6, 2016 at 9:20

1 Answer 1

1

I found something that prolongs the session in \Magento\Setup\Controller\Session::prolongAction() but it's only for the setup wizard.
So I stopped looking and built my own.
I my module I added a new controller action:

namespace [Namespace]\[Module]\Controller\Adminhtml\[Entity];

use Magento\Backend\App\Action;

class Prolong extends Action
{
    /**
     * prolong session
     *
     * @return void
     */
    public function execute()
    {
        $this->getResponse()->setBody(json_encode(['success' => true]));
    }
}

Then created a simple block and template that can be included in any page.

namespace [Namespace]\[Module]\Block\Adminhtml;

use Magento\Backend\Block\Template;

class SessionProlong extends Template
{
    /**
     * @return string
     */
    public function getSessionProlongUrl()
    {
        return $this->_urlBuilder->getUrl('[module]/[controller]/prolong');
    }
}

and the template session_prolong.phtml:

<?php /** @var \[Namespace]\[Module]\Block\Adminhtml\SessionProlong $block */ ?>
<script type="text/javascript">
    require(["jquery"],
        function(jQuery) {
            jQuery(document).ready(function() {
                setInterval(function(){
                    jQuery.ajax({
                        type: 'GET',
                        url: '<?php /** @noEscape */ echo $block->getSessionProlongUrl() ?>'
                    });
                }, 60000)
            });
        }
    );
</script>

The block can now be added in any admin page through the layout file:

<body>
    <referenceContainer name="content">
        <block class="[Namespace]\[Module]\Block\Adminhtml\SessionProlong" name="session_prolong" template="[Namespace]_[Module]::session_prolong.phtml"/>
    </referenceContainer>
</body>

When the block and template is present in the page, an AJAX call will be made ever 60 seconds to my controller action, that does nothing else than prolonging the session.
I left the browser open over night and I didn't get logged out, so I can conclude it works.

1
  • Not working, got ajaxExpired: 1 error in response. and admin session timeout. Jul 25, 2021 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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