Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Extend Magento 2 Admin Login Time

Every store owner knows how frustrating it is when you constantly have to log in to the Magento 2 Admin panel because the session has expired. Extending the M2 admin cookie time is very easy to do luckily, and it will save you a bunch of time that can be better spent elsewhere. It’s fairly easy to do, but there’s a small difference between Magento 2.0 and 2.1. We will show you both solutions below.

Magento 2.0

To change the backend session time, navigate to the following pages in the admin panel.

Stores -> Settings -> Configuration -> Advanced -> Admin -> Security -> Admin Session Lifetime. The session lifetime is specified in seconds as you can probably guess from the high number it’s set on by default. Change it to the number of seconds you would like to stay logged in and hit save.

You can also change the admin session time directly in the MySQL database. This can be done by looking for the “core_config_data” table and changing the “admin/security/session_lifetime” path.

Magento 2.1

In version 2.1 of Magento, the admin login cookie is changed to a “session”. This enables store owners to set a specified time in the backend by navigating to the page we outlined above, OR they can choose “when admin closes browser”. This means the session will automatically end when the admin panel browser/tab is closed. It doesn’t act like a cookie anymore like it did in 2.0. You can change the code by doing the following.

This code you are looking for is displayed in Magento\Backend\Model\Session\AdminConfig.

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

To change it, simply create a plugin with the following interceptor method for the function.

public function beforeSetCookieLifetime()
{
    $lifetime = $this->scopeConfig->getValue(
        \Magento\Framework\Session\Config::XML_PATH_COOKIE_LIFETIME,
        \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
    return [$lifetime, \Magento\Framework\Session\Config::COOKIE_LIFETIME_DEFAULT];
}

Where $this->scopeConfig is an instance of Magento\Framework\App\Config\ScopeConfigInterface, injected via a constructor parameter. Now the cookie lifetime is used from the configuration like it was before.

We hope this tutorial has helped you! If you have any questions, please ask them in the comment form below for everyone to see, and we’ll answer them.

The post Extend Magento 2 Admin Login Time appeared first on Coding Basics.



This post first appeared on Coding Basics - Programming & Magento Tutorials, please read the originial post: here

Share the post

Extend Magento 2 Admin Login Time

×

Subscribe to Coding Basics - Programming & Magento Tutorials

Get updates delivered right to your inbox!

Thank you for your subscription

×