title: Zend_Auth - Be Able To Login With Both Email And Username
body: <p>So I am using Zend_Auth to authenticate users of my website. Currently they are only able to log in with their email as login but I would like to enable them to log in also with their username.</p>
  
  <p>Here is some code:</p>
  
  <pre><code>        // prepare adapter for Zend_Auth
          $adapter = new Zend_Auth_Adapter_DbTable($this-&gt;_getDb());
          $adapter-&gt;setTableName('users');
          $adapter-&gt;setIdentityColumn('email');
          $adapter-&gt;setCredentialColumn('password_hash');
          $adapter-&gt;setCredentialTreatment('CONCAT(SUBSTRING(password_hash, 1, 40), SHA1(CONCAT(SUBSTRING(password_hash, 1, 40), ?)))');
          $adapter-&gt;setIdentity($request-&gt;getParam('email'));
          $adapter-&gt;setCredential($request-&gt;getParam('password'));
  </code></pre>
  
  <p>Notice the line:</p>
  
  <pre><code>        $adapter-&gt;setIdentityColumn('email');
  </code></pre>
  
  <p>How can I add also username there (column in the database called username, too)?</p>
  
  <p>UPDATE:</p>
  
  <p>This is how I solved this:</p>
  
  <pre><code>        $login = $request-&gt;getParam('email');
          $validator = new Zend_Validate_EmailAddress();
          if (false === $validator-&gt;isValid($login)) {
              $u = $this-&gt;_getTable('Users')-&gt;getSingleWithUsername($login);
              if (null === $u) {
                  throw new Exception ('Invalid login and/or password');
              }
              $login = $u-&gt;email;
          }
  
          // prepare adapter for Zend_Auth
          $adapter = new Zend_Auth_Adapter_DbTable($this-&gt;_getDb());
          $adapter-&gt;setTableName('users');
          $adapter-&gt;setIdentityColumn('email');
          $adapter-&gt;setCredentialColumn('password_hash');
          $adapter-&gt;setCredentialTreatment('CONCAT(SUBSTRING(password_hash, 1, 40), SHA1(CONCAT(SUBSTRING(password_hash, 1, 40), ?)))');
          $adapter-&gt;setIdentity($login);
          $adapter-&gt;setCredential($request-&gt;getParam('password'));
  </code></pre>