2

In default Magento 2.0.7:

A customer is logged in, closes the browser, reopens the browser and he is still logged in.

But I would like to log out the customer when he closes the browser. He should be redirected to the login page if he reopens the browser.

Please suggest your ideas to implement this.

4

Solution for customers

Set cookie lifetime to 0 ("session") in Stores > Configuration > General > Web:

screenshot

He should be redirected to the login page if he reopens the browser.

If you don't want to allow not logged in customers to view any page besides the login page at all, you'll need an extension that can restrict access to the catalog.

Solution for admin

In Magento 2.1 this is already the case for the admin area: session cookies are set with "session" lifetime.

The relevant code is in Magento\Backend\Model\Session\AdminConfig:

/**
 * Set session cookie lifetime to session duration
 *
 * @return $this
 */
protected function configureCookieLifetime()
{
    return $this->setCookieLifetime(0);
}

Without rewriting the class, you can achieve the same in Magento 2.0 with a plugin for this class and the following interceptor method:

public function beforeSetCookieLifetime()
{
    return [0, null];
}

This way the cookie lifetime is always set to 0 (i.e. "session"), regardless what's the configured value.

  • Hi @fschmengler, thank you for your comment and it works. – Gideon Sep 7 '16 at 5:08
  • Hi if I want to include remember me / keep me logged in feature and log out a customer when the customer is closing the browser in Magento 2.2.2, how can I achieve this? – Gideon Jul 4 '18 at 7:23

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.