title: How to bind user object to request in a middleware
body: <p>i'm writing an application in Laravel Spark 1.0 (Laravel 5.2). I wrote a custom middleware for agent (api) authentication. This is the code:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use App\Agent;
  use Closure;
  use Illuminate\Http\Request;
  
  class AgentAuth
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
          if( isset($request-&gt;token) &amp;&amp;  !empty($request-&gt;token) )
          {
              $agent = Agent::where('token', '=', $request-&gt;token)-&gt;first();
  
              if( $agent != NULL )
              {
                  $team = $agent-&gt;Team()-&gt;first();
                  $user = $team-&gt;User()-&gt;first();
  
                  $request-&gt;merge(['team' =&gt; $team ]);
                  $request-&gt;merge(['user' =&gt; $user ]);
  
                  return $next($request);
              }
              else {
                  return response('Unauthorized 2.', 401);
              }
  
          }
          else {
              return response('Unauthorized 1.', 401);
          }
      }
  }
  </code></pre>
  
  <p>In the default laravel authentication the user object is injected in the request (see laravel docs): <a href="https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user" rel="noreferrer">https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user</a></p>
  
  <p>So you can retrieve the user using:</p>
  
  <pre><code>$request-&gt;user();
  </code></pre>
  
  <p>Spark obviously use this method to check if user subscription is valid (laravel\spark\src\Http\Middleware\VerifyUserIsSubscribed):</p>
  
  <pre><code>if ($this-&gt;subscribed($request-&gt;user(), $subscription, $plan, func_num_args() === 2)) {
              return $next($request);
          }
  </code></pre>
  
  <p>And it's not working because, with my middleware, you can retrieve the user using: <code>$request-&gt;user;</code> but not with the laravel defaults <code>$request-&gt;user();</code></p>
  
  <p>How should i inject the user object into the request?</p>
  
  <p>Thank you in advance</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Laravel in the service provider (Illuminate\Auth\AuthServiceProvider@registerRequestRebindHandler)</p>
  
  <p>Use this code to bind object user to the request:</p>
  
  <pre><code>/**
       * Register a resolver for the authenticated user.
       *
       * @return void
       */
      protected function registerRequestRebindHandler()
      {
          $this-&gt;app-&gt;rebinding('request', function ($app, $request) {
              $request-&gt;setUserResolver(function ($guard = null) use ($app) {
                  return call_user_func($app['auth']-&gt;userResolver(), $guard);
              });
          });
      }
  </code></pre>
  
  <p>I tried to insert this code, with the appropriate correction, in the middleware but i can't figure out how to make it work.</p>