1

I have time in seconds when the session should end. If the user has not selected the checkbox "remember_me" - the session will last 2 hours. When the checkbox is selected - should last - 48 hours. I have a loginСontroller, where I react - to the login result and if the validation is successful and checkbox = "on" you need to change the session time. I tried to change by looking at the documentation, and spent a lot of time looking for a solution. Maybe someone can help me. Thank you very much in advance[enter image description here]

here is my file config/app.php

'Session' => [
    'defaults' => 'php',
     'ini' => [
         'session.cookie_lifetime' => 7200,
     ]
],

and here is my loginController

    `public function index()
    {
    $this->viewBuilder()->setLayout('main');
    $this->set("title", "");
    $this->set("description", "description");
    $this->request->allowMethod(['get', 'post']);

    $result = $this->Authentication->getResult();
    // regardless of POST or GET, redirect if user is logged in
    if ($result->isValid()) {
        if ($this->request->getData('remember') == 'on') {
            ///// the solution should be here
        }
        $redirect = [
            'controller' => 'Main',
            'action' => 'index',
        ];
        return $this->redirect($redirect);
    }
    // display error if user submitted and authentication failed
    if ($this->request->is('post') && !$result->isValid()) {
        $this->Flash->saved_error(__('Invalid email or password'));
    }
}`
ndm
  • 59,784
  • 9
  • 71
  • 110

1 Answers1

1

You most likely shouldn't do it that way, your controller code shouldn't have to know about such details if it can be avoided.

The authentication plugin ships with a cookie based authenticator that you can use in addition to the session authenticator, that way you can extend authentication beyond the default session lifetime, I'd suggest that you look into that instead.

$service->loadAuthenticator('Authentication.Cookie', [
    'fields' => $fields,
    'loginUrl' => $loginUrl,
    'cookie' => [
        // cookie expires in 2 days from now
        'expires' => \Cake\Chronos\Chronos::now()->addDays(2)
    ],
]);

By default the authenticator looks up a field named remember_me, so either rename that in your template, like:

echo $this->Form->control('remember_me', ['type' => 'checkbox']);

or configure the authenticator's rememberMeField option with the custom field name that you're using in your form.

See also

ndm
  • 59,784
  • 9
  • 71
  • 110