title: Authentication using Cakephp and PhoneGap
body: <p>I am working on an application using Cakephp on the server side and PhoneGap at the client Side, with JSON as a intermediate to access the server side. </p>
  
  <p>Now, I am working specifically on a login form where the user needs to enter his/her username and password. I put in my <code>controller</code> the following: </p>
  
  <pre><code>public function api_login() {
  
      header("Access-Control-Allow-Origin: *");
      header("Access-Control-Allow-Headers: Authorization");
  
      if ($this-&gt;request-&gt;data &amp;&amp; isset($this-&gt;request-&gt;data['username']) &amp;&amp; isset($this-&gt;request-&gt;data['password'])) {
          $arrUser = $this-&gt;User-&gt;find('all',array(
              'conditions'=&gt;array(
                  'username'=&gt; $this-&gt;request-&gt;data['username'],
                  'password' =&gt; $this-&gt;request-&gt;data['password']
              )
          ));
  
          if (count($arrUser) &gt; 0 ) {
              $this-&gt;Session-&gt;write('Auth.User',$arrUser[0]['User']);
              $arrReturn['status'] = 'SUCCESS';
              $arrReturn['data'] = array('loginSuccess' =&gt; 1,'user_id' =&gt; $arrUser[0]['User']['id'] );
  
          }
          else {
              $arrReturn['status'] = 'NOTLOGGEDIN';
              $arrReturn['data'] = array( 'loginSuccess' =&gt; 0 );
          }
      } else {
          $arrReturn['status'] = 'NOTLOGGEDIN';
          $arrReturn['data'] = array( 'loginSuccess' =&gt; 0 );
      }
      echo json_encode($arrReturn);
  }
  </code></pre>
  
  <p>and in the client side, I am retrieving what JSON encoded as follows:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function(){
          $('form').on('submit',function(e){
              e.preventDefault();
              $username = $("#form-username").val();
              $password = $("#form-password").val();
              $.ajax({
                  url : "http://localhost/teslaphonegap_cakephp/" + 'login.json',
                  cache : false,
                  data : {
                      'username' : $username,
                      'password' : $password },
                  dataType : 'json',
                  type : 'POST',
                  success : function(result) {
  
                      if(result.status=="SUCCESS"){
                          alert("success");
                          console.log(result);
                      }else{
  
                          alert("username or pass are wrong");
                          console.log(result);
                      } },
                  error : function(xhr, status, err) {
                      alert("ERROR");
                  }
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>and in my <code>Model</code> I used the <code>beforeSave()</code> in order to hash the passwords before they get added at the very beginning in the Database:</p>
  
  <pre><code>public function beforeSave($options = array()) {
  
      $value=$this-&gt;data['User']['password'];
      $encrypted = Security::encrypt($value, Configure::read('Security.cipherCriptKey'));
      $this-&gt;data['User']['password'] = $encrypted;
      return true;
  } 
  </code></pre>
  
  <p>Now, when I try to login it always returns the error message because it compares a value that is unhashed with other values that are already hashed in my Database. How can I solve this issue? I used the <code>afterFind()</code> but it didn't work:</p>
  
  <pre><code>public function afterFind($results, $primary = false) {
  
      foreach ($results as $key =&gt; $val) {            
          if(isset($val['User']['password'])){
              $results['User']['password'] = Security::decrypt($val['User']['password'], Configure::read('Security.cipherCriptKey'));
          }
          return $results;
      }        
  }
  </code></pre>
  
  <p>-- EDIT</p>
  
  <p>and in my <code>core.php</code> I used the following:</p>
  
  <pre><code>    Configure::write('Security.cipherCriptKey','su0HKssPmdbwgK6LdQLqzp0Y7zOmyaTI');
  </code></pre>