title: Use cache in auth middleware
body: <p>I'd like to write own auth  route middleware to use user cache. If I will simple check with cache in DB user then I see </p>
  
  <p><code>Call to a member function setCookie() on null</code></p>
  
  <p>I should use UserProvider probably. But then I don't use cache.</p>
  
  <p>Does it any way to return same result like original auth but set cache inside?</p>
  
  <p>My current code is:</p>
  
  <pre><code>&lt;?php
  namespace Sakuicms\Auth\Middleware;
  
  use Closure;
  use Illuminate\Contracts\Auth\Factory as Auth;
  use Illuminate\Auth\AuthenticationException;
  
  
  class Authenticate
  {
      protected $auth;
  
      public function __construct(Auth $auth)
      {
        $this-&gt;auth = $auth;
      }
  
      protected function getName()
      {
        return 'login_'.config('auth.defaults.guard').'_'.sha1('Illuminate\Auth\SessionGuard');
      }
  
      public function handle($request, Closure $next, ...$guards)
      {
        $id = session($this-&gt;getName());
        if($id&gt;0){
          $model = config('auth.providers.users.model');
          return \Cache::rememberForever('db.users.'.$id, function() use($model,$id){
            return $model::select(['id'])-&gt;where('id',$id)-&gt;first();
          });
        }
        throw new AuthenticationException('Unauthenticated.', $guards);
        return $next($request);
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
  I solve this problem. Of course it was my wrong. I always return not <code>$next($request)</code> but clear user object</p>
  
  <p>Below is code which work</p>
  
  <pre><code>&lt;?php
  namespace Sakuicms\Auth\Middleware;
  
  use Closure;
  use Illuminate\Contracts\Auth\Factory as Auth;
  use Illuminate\Auth\AuthenticationException;
  use Sakuicms\Auth\SessionGuard;
  
  class Authenticate
  {
      protected $auth;
  
      public function __construct(Auth $auth)
      {
        $this-&gt;auth = $auth;
      }
  
      protected function getName()
      {
        return 'login_'.config('auth.defaults.guard').'_'.sha1('Illuminate\Auth\SessionGuard');
      }
  
      public function handle($request, Closure $next, ...$guards)
      {
        $this-&gt;authenticate($guards);
        return $next($request);
      }
  
      protected function authenticate(array $guards)
      {
        if(empty($guards)){
          $id = session($this-&gt;getName());
          if($id&gt;0){
            $model = config('auth.providers.users.model');
            return \Cache::rememberForever('db.users.'.$id, function() use($model,$id){
              return $model::select(['id'])-&gt;where('id',$id)-&gt;first();
            });
          }
        }
  
        foreach ($guards as $guard) {
            if ($this-&gt;auth-&gt;guard($guard)-&gt;check()) {
                return $this-&gt;auth-&gt;shouldUse($guard);
            }
        }
  
        throw new AuthenticationException('Unauthenticated.', $guards);
      }
  
  }
  </code></pre>
  
  <p>Probably exists better/more elegant solution but for now it's ok for me. I'll refactor it in next stage.</p>