title: Login with email address or username in CakePHP v2.0
body: <p>In CakePHP prior to 2.0 you could allow a user to login using their email address by stopping the autoRedirect and then comparing the username data to the email column in your database (apparently Cake could then fallback to username checks if not an email).</p>
  
  <p>In CakePHP 2.0 this has changed and you login manually using <code>$this-&gt;Auth-&gt;login()</code></p>
  
  <p>My question is how do I get this working for 2.0? I have some quite complicated code that does a variety of things such as handle ajax and postback requests, locking of accounts if a user tries to login too many times etc so it's quite long!</p>
  
  <p>As you will see I check if the account actually exists manually so I can show a message of account not found before going through the authentication process if the case, and also use this to lock the account of that user if 5 fail attempts.</p>
  
  <p>The main problem here is allowing the system to check on both usernames and email addresses for authentication, the system in place does LOCK the user if you use the email address as it handles that in the mentioned check, but it will always fail because the authentication cannot handle it.</p>
  
  <p>Hope someone can help, offer ideas tips. Thanks</p>
  
  <pre><code>if ($this-&gt;request-&gt;is('post'))
          {       
              $opts = array(
                 'conditions'=&gt;array(
                     'OR'=&gt;array(
                        'User.username'=&gt;$this-&gt;data['User']['username'],
                        'User.email'=&gt;$this-&gt;data['User']['username']
                      )
                  )
              );
  
              $user = $this-&gt;User-&gt;find('first', $opts);
  
              if(!empty($user))
              {
                  if($user['User']['status'] == 0)
                  {
                      if($this-&gt;request-&gt;is('ajax'))
                      {
                          $this-&gt;autoRender = false;
                          echo json_encode(array('authenticated'=&gt;false,'error'=&gt;__('Sorry your account is currently locked. Please reset your password.?')));
                      }
                      else
                      {
                          $this-&gt;Session-&gt;setFlash(__('Sorry your account is currently locked. Please reset your password.'), 'default', array(), 'auth');
                      }
                  }
                  else
                  {   
                      if ($this-&gt;Auth-&gt;login())
                      {   
                          if ($this-&gt;request-&gt;is('ajax'))
                          {   
                              $this-&gt;autoRender = false;
                              if(isset($this-&gt;params['url']['continue']))
                              {
                                  $pathtoredirect = $this-&gt;UrlEncode-&gt;base64url_decode($this-&gt;params['url']['continue']);
  
                                  echo json_encode(array('authenticated'=&gt;true,'redirect'=&gt;$pathtoredirect,'base'=&gt;false));
                              }
                              else
                              {
                                  $pathtoredirect = $this-&gt;Auth-&gt;redirect();
  
                                  echo json_encode(array('authenticated'=&gt;true,'redirect'=&gt;$pathtoredirect,'base'=&gt;true));
                              }
                          }
                          else
                          {
                              if(isset($this-&gt;params['url']['continue']))
                              {
                                  $pathtoredirect = $this-&gt;UrlEncode-&gt;base64url_decode($this-&gt;params['url']['continue']);
                              }
                              else
                              {
                                  $pathtoredirect = $this-&gt;Auth-&gt;redirect();
                              }
                              return $this-&gt;redirect($pathtoredirect);
                          }               
                      }
                      else
                      {               
                          if($this-&gt;Session-&gt;read('attempts'))
                          {
                              $attempts = $this-&gt;Session-&gt;read('attempts') + 1;
                          }
                          else
                          {
                              $attempts = 1;
                          }
  
                          $this-&gt;Session-&gt;write('attempts', $attempts);
  
                          if($attempts &gt;= 5)
                          {
                              $this-&gt;User-&gt;id = $user['User']['id'];
                              $this-&gt;User-&gt;saveField('status', 0);
                              if ($this-&gt;request-&gt;is('ajax'))
                              {
                                  $this-&gt;autoRender = false;
                                  echo json_encode(array('authenticated'=&gt;false,'error'=&gt;__('Username or password is incorrect. For security reasons this account has now been locked and you must reset your password to unlock it.')));
                              }
                              else
                              {
                                  $this-&gt;Session-&gt;setFlash(__('Username or password is incorrect. For security reasons this account has now been locked and you must reset your password to unlock it.'), 'default', array(), 'auth');
                              }
                          }
                          else
                          {
                              if ($this-&gt;request-&gt;is('ajax'))
                              {
                                  $this-&gt;autoRender = false;
                                  echo json_encode(array('authenticated'=&gt;false,'error'=&gt;__('Username or password is incorrect')));
                              }
                              else
                              {
                                  $this-&gt;Session-&gt;setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
                              }
                          }
                      }
                  }
              }
              else
              {
                  if ($this-&gt;request-&gt;is('ajax'))
                  {
                      $this-&gt;autoRender = false;
                      echo json_encode(array('authenticated'=&gt;false,'error'=&gt;__('Sorry that account does not exist.')));
                  }
                  else
                  {
                      $this-&gt;Session-&gt;setFlash(__('Sorry that account does not exist.'), 'default', array(), 'auth');
                  }
              }
          }
  </code></pre>