Auth – opcje

// Parametry logowania zalogowanego użytkownika
$email   = $this->Authentication->getIdentity()->get('email');
$user_id = $this->Authentication->getIdentity()->get('id');
$user_id = $this->Authentication->getIdentifier();


$user_id = $this->request->getAttribute('identity')->getIdentifier();

Rezultat autentykacji dostępny w metodach kontrolera

$this->request->getAttribute('authentication')

UsersController.php

public function beforeFilter(\Cake\Event\EventInterface $event)
{
  parent::beforeFilter($event);
  // Pozwala na dostęp do strony logowania lub rejestracji bez zalogowania
  $this->Authentication->addUnauthenticatedActions(['login', 'add']);
}

public function login()
{
    $this->request->allowMethod(['get', 'post']);
    $result = $this->Authentication->getResult();

    // POST czy GET, przekieruj jeśli user jest zalogowany
    if ($result->isValid()) {
        $redirect = $this->request->getQuery('redirect', [
            'controller' => 'Articles',
            'action'     => 'index',
        ]);

        return $this->redirect($redirect);
    }
    // Przesłano formularz a user nie zalogowany - błąd
    if ($this->request->is('post') && !$result->isValid()) {
        $this->Flash->error(__('Niepoprawny login lub hasło'));
    }
}
public function logout()
{
    $result = $this->Authentication->getResult();
    if ($result->isValid()) {
        $this->Authentication->logout();
        return $this->redirect(['controller' => 'Users', 'action' => 'login']);
    }
}