title: Cakephp 3.0 Login
body: <p>I'm facing trouble with my login system. I managed to register my user in my database but whenever I try to log in, it keeps prompting "Invalid email or password, try again".</p>
  
  <p>This is my model:</p>
  
  <pre><code>    &lt;?php
      namespace App\Model\Table;
  
      use Cake\ORM\Table;
      use Cake\Validation\Validator;
  
      class UsersTable extends Table
      { 
  
      public function validationDefault(Validator $validator)
      {
      return $validator
          -&gt;notEmpty('email', 'A email is required')
          -&gt;add('email', 'valid' , ['rule'=&gt; 'email'])
          -&gt;add('email', [
              'unique' =&gt; ['rule' =&gt; 'validateUnique', 'provider' =&gt; 'table']
          ])
          -&gt;requirePresence('email','create')
  
          -&gt;notEmpty('password', 'A password is required')
          -&gt;notEmpty('role', 'A role is required')
          -&gt;add('role', 'inList', [
              'rule' =&gt; ['inList', ['admin', 'author']],
              'message' =&gt; 'Please enter a valid role'
          ]);
          } 
  
          }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>       &lt;?php
         namespace App\Controller;
  
         use App\Controller\AppController;
         use Cake\Event\Event;
         use Cake\Network\Exception\NotFoundException;
  
         class UsersController extends AppController
         {
  
         public function beforeFilter(Event $event)
         {
         parent::beforeFilter($event);
  
      $this-&gt;Auth-&gt;allow(['add', 'logout']);
     }
  
      public function login()
     {
      if ($this-&gt;request-&gt;is('post')) {
      $user = $this-&gt;Auth-&gt;identify();
      if ($user) {
          $this-&gt;Auth-&gt;setUser($user);
          return $this-&gt;redirect($this-&gt;Auth-&gt;redirectUrl());
      }
      $this-&gt;Flash-&gt;error(__('Invalid email or password, try again'));
    }
    }
  
    public function logout()
    {
    return $this-&gt;redirect($this-&gt;Auth-&gt;logout());
    }
  
   public function index()
   {
      $this-&gt;set('users', $this-&gt;Users-&gt;find('all'));
  }
  
  public function view($id)
  {
      if (!$id) {
          throw new NotFoundException(__('Invalid user'));
      }
  
      $user = $this-&gt;Users-&gt;get($id);
      $this-&gt;set(compact('user'));
  }
  
  public function add()
  {
      $user = $this-&gt;Users-&gt;newEntity();
      if ($this-&gt;request-&gt;is('post')) {
          $user = $this-&gt;Users-&gt;patchEntity($user, $this-&gt;request-&gt;data);
          if ($this-&gt;Users-&gt;save($user)) {
              $this-&gt;Flash-&gt;success(__('The user has been saved.'));
              return $this-&gt;redirect(['action' =&gt; 'add']);
          }
          $this-&gt;Flash-&gt;error(__('Email already existed.'));
      }
      $this-&gt;set('user', $user);
     } 
  
      }
  </code></pre>
  
  <p>AppController:</p>
  
  <pre><code>    &lt;?php
  
      namespace App\Controller;
  
      use Cake\Controller\Controller;
      use Cake\Event\Event;
  
  
     class AppController extends Controller
     {
  
  
    public function initialize()
    {
       $this-&gt;loadComponent('Flash');
      $this-&gt;loadComponent('Auth', [
          'authorize' =&gt; ['Controller'],  
          'loginRedirect' =&gt; [
              'controller' =&gt; 'Articles',
              'action' =&gt; 'index'
          ],
          'logoutRedirect' =&gt; [
              'controller' =&gt; 'Pages',
              'action' =&gt; 'display',
              'home'
          ]
      ]);
    }
  
    public function isAuthorized($user)
  {
  
  if (isset($user['role']) &amp;&amp; $user['role'] === 'admin') {
      return true;
  }
  
  
  return false;
    }
  
  public function beforeFilter(Event $event)
  {
      $this-&gt;Auth-&gt;allow(['index', 'view', 'display']);
  }
  
   } 
  </code></pre>
  
  <p>login.ctp</p>
  
  <pre><code> &lt;div class="users form"&gt;
   &lt;?= $this-&gt;Flash-&gt;render('auth') ?&gt;
  &lt;?= $this-&gt;Form-&gt;create() ?&gt;
  &lt;fieldset&gt;
      &lt;legend&gt;&lt;?= __('Please enter your username and password') ?&gt;&lt;/legend&gt;
      &lt;?= $this-&gt;Form-&gt;input('email') ?&gt;
      &lt;?= $this-&gt;Form-&gt;input('password') ?&gt;
  &lt;/fieldset&gt;
  &lt;?= $this-&gt;Form-&gt;button(__('Login')); ?&gt;
   &lt;?= $this-&gt;Form-&gt;end() ?&gt;
  &lt;/div&gt;  
  </code></pre>