title: Authenticate Against Database with Phil Sturgeon’s Rest-Server
body: <p>I'm trying to modify Phil Sturgeon's rest-server to authenticate against ion-auth. I changed _check_login to:</p>
  
  <pre><code>private function _check_login($username = '', $password = NULL)
  {
      if (empty($username))
      {
          return FALSE;
      }
  
      $this-&gt;rest-&gt;db = $this-&gt;load-&gt;database('default', TRUE);
      $sql = 'SELECT api_key FROM users WHERE email = ?';
      $query = $this-&gt;rest-&gt;db-&gt;query($sql, urldecode($username));
  
      if ($query-&gt;num_rows() &gt; 0) {
          $user = $query-&gt;row_array();
      } else {
          return FALSE;
      }
  
      // If actually NULL (not empty string) then do not check it
      if ($password !== NULL AND $user['api_key'] != $password)
      {
          return FALSE;
      }
  
      //return TRUE;
      return $user['api_key'];
  }
  </code></pre>
  
  <p>My first question is do I need to do:</p>
  
  <pre><code>$this-&gt;rest-&gt;db = $this-&gt;load-&gt;database('default', TRUE);
  </code></pre>
  
  <p>It seems to me like $this->rest->db should already be accessible since it's set in the constructor however I get an error saying I'm trying to query on a non-object if I don't add the load->database() line in _check_login like above. NOTE: I removed the if statement in the constructor that only initializes the db connection if keys or logging are enabled so this shouldn't be the issue.</p>
  
  <p>My second issue I think is a result of question 1. Anyhow, after making the changes to REST_Controller my models in my REST controllers no longer work. The error I'm getting is:</p>
  
  <blockquote>
    <p>A PHP Error was encountered</p>
    
    <p>Severity: Notice</p>
    
    <p>Message: Undefined property: User::$db</p>
    
    <p>Filename: core/Model.php</p>
    
    <p>Line Number: 50</p>
  </blockquote>
  
  <p>which appears to be occurring when I make a call to a method in one of my model's.</p>
  
  <p>Hopefully that's enough info to help pinpoint the problem. Any ideas on what I might be doing wrong? Thanks!</p>