title: Laravel 4 - How to implement a custom Authentication Provider for a REST API accessed from a mobile app?
body: <p>I am building a REST API using Laravel 4. This API will be accessed from a mobile app. I want to extend Laravel's built in Authentication service to use tokens.</p>
  
  <p>The mobile app calls the API for a certain user to the endpoint <code>users/login</code>.</p>
  
  <p>It then generates a new token and inserts it in the token table for the corresponding user id. See the structure of the token table below :</p>
  
  <pre><code>id | userId | token | created_at | updated_at | expires_at
  </code></pre>
  
  <p>I want to write an Authentification Provider to override the <code>retrieveByID</code>, <code>retrieveByCredentials</code> and <code>validateCredentials</code> functions.</p>
  
  <p>This for the mobile app to call any API methods with the token in its header but without any user credentials.</p>
  
  <p>I am building it locally using HTTP but then it will be HTTPS.</p>
  
  <p>I followed different tutorials and wrote the <code>retrieveByCredentials</code> function like this :</p>
  
  <pre><code>public function retrieveByCredentials(array $credentials) {
  
      $token = token::where('token', '=', $credentials['token'])-&gt;take(1)-&gt;get();
      if ($token) {
          $user = User::where('user', '=', $token-&gt;userId)-&gt;get();
          return new GenericUser($user);
      }
      else {
          return false;
      }
  
  }
  </code></pre>
  
  <p>I declared </p>
  
  <pre><code>use token;
  use user;
  </code></pre>
  
  <p>At the beginning of my AuthUserProvider class but I guess it is not the way to do it.</p>
  
  <p>The code returns :</p>
  
  <pre><code>Undefined property: Illuminate\Database\Eloquent\Collection::$userId 
  </code></pre>
  
  <p>What I am missing I guess, is how to access my Models in this class so that I can retrieve Users and validate Tokens. Could you help?</p>