title: Laravel session timeout, extra logout code
body: <p><strong>Bottom line:</strong></p>
  
  <p>How to logout the user on session time out?</p>
  
  <hr>
  
  <p><strong>Detailed Question:</strong></p>
  
  <p>I have a Laravel 5.6.* application and the project demands the user to logout whenever the user is idle. I have tried the solutions that are given here, but none of them worked for me.</p>
  
  <p>Then I stumbled upon this post:
  <a href="https://laravel-tricks.com/tricks/session-timeout-for-logged-in-user" rel="nofollow noreferrer">https://laravel-tricks.com/tricks/session-timeout-for-logged-in-user</a> and made my way through it to no success.</p>
  
  <hr>
  
  <p><strong>What I want:</strong></p>
  
  <p>Logout the user automatically on session timeout. Before logging out, set <code>is_logged_in</code> attribute to <code>false</code> or <code>0</code> on the <code>Users</code> table. How do I achieve this?</p>
  
  <hr>
  
  <p><strong>Code that I have tried so far:</strong></p>
  
  <p><code>session.php</code></p>
  
  <pre><code>/*
   |--------------------------------------------------------------------------
   | Session Lifetime
   |--------------------------------------------------------------------------
   |
   | Here you may specify the number of minutes that you wish the session
   | to be allowed to remain idle before it expires. If you want them
   | to immediately expire on the browser closing, set that option.
   |
   */
  
   'lifetime' =&gt; env('SESSION_LIFETIME', 120),
  
   'expire_on_close' =&gt; false,
  </code></pre>
  
  <p><code>SessionTimeOut.php</code> Middleware</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Closure;
  use App\Traits\CacheQueryResults;
  
  class SessionTimeOut
  {
      use CacheQueryResults;
  
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
          // session()-&gt;forget('lastActivityTime');
  
          if (! session()-&gt;has('lastActivityTime')) {
              session(['lastActivityTime' =&gt; now()]);
          }
  
          // dd(
          //     session('lastActivityTime')-&gt;format('Y-M-jS h:i:s A'),
          //     now()-&gt;diffInMinutes(session('lastActivityTime')),
          //     now()-&gt;diffInMinutes(session('lastActivityTime')) &gt;= config('session.lifetime')
          // );
  
          if (now()-&gt;diffInMinutes(session('lastActivityTime')) &gt;= (config('session.lifetime') - 1) ) {
              if (auth()-&gt;check() &amp;&amp; auth()-&gt;id() &gt; 1) {
                 $user = auth()-&gt;user();
                 auth()-&gt;logout();
  
                 $user-&gt;update(['is_logged_in' =&gt; false]);
                 $this-&gt;reCacheAllUsersData();
  
                 session()-&gt;forget('lastActivityTime');
  
                 return redirect(route('users.login'));
             }
  
         }
  
         session(['lastActivityTime' =&gt; now()]);
  
         return $next($request);
      }
  }
  
  </code></pre>
  
  <p><code>Kernel.php</code></p>
  
  <pre><code>/**
   * The application's route middleware groups.
   *
   * @var array
   */
   protected $middlewareGroups = [
       'web' =&gt; [
           \App\Http\Middleware\EncryptCookies::class,
           \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
           \Illuminate\Session\Middleware\StartSession::class,
           // \Illuminate\Session\Middleware\AuthenticateSession::class,
           \Illuminate\View\Middleware\ShareErrorsFromSession::class,
           \App\Http\Middleware\VerifyCsrfToken::class,
           \Illuminate\Routing\Middleware\SubstituteBindings::class,
           \App\Http\Middleware\SessionTimeOut::class,
       ],
  ];
  </code></pre>