title: Laravel 5 - Checking if the user session has expired
body: <p>I am trying to implement a way to check if the user session has expired and then pop-up a modal letting the user know.</p>
  
  <p>Here is what I have so far.</p>
  
  <p>Route</p>
  
  <pre><code>Route::get('auth-check', ['uses' =&gt; 'UsersController@authCheck', 'as' =&gt; 'auth.check']);
  </code></pre>
  
  <p>UsersController</p>
  
  <pre><code>use Illuminate\Contracts\Auth\Guard;
  
  class UsersController extends Controller {
  
      public function __construct(Guard $auth)
      {
          $this-&gt;auth = $auth;
      }
  
  ...
  
      /*
      * returns {status: false} or {status: true}
      */
      public function authCheck()
      {
          return response(['status' =&gt; $this-&gt;auth-&gt;check()])
                        -&gt;header('Content-Type', 'application/json');
      }
  
  }
  </code></pre>
  
  <p>Master blade (so it can be checked on any page)</p>
  
  <pre><code>&lt;!-- Modal --&gt;
  &lt;div class="modal fade" id="session-expired"&gt;
      &lt;div class="modal-dialog"&gt;
          &lt;div class="modal-content"&gt;
              &lt;div class="modal-header"&gt;
                  &lt;h4 class="modal-title"&gt;Session Expired!&lt;/h4&gt;
              &lt;/div&gt;
              &lt;div class="modal-body"&gt;
                  &lt;p&gt;Your session has expired, please login to continue.&lt;/p&gt;
              &lt;/div&gt;
              &lt;div class="modal-footer"&gt;
                  &lt;a href="{!! route('login') !!}" class="btn btn-danger"&gt;Login&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>@if(Auth::check())
  $.fn.timer = function()
  {
      $.get("{{ route('auth.check') }}", function (data) {
          data.status == false ? $('#session-expired').modal({ show: true, backdrop: 'static' }) : $('#session-expired').modal('hide');
      });
  };
  
  window.setInterval(function(){
      $('#timedOutSession').timer();
  },10000);
  @endif
  </code></pre>
  
  <p>It kinda works, when I am logged in it returns <code>{status: true}</code>. But after 5-10 requests on checking the auth user the session dies for some reason and it returns <code>{status: false}</code>. Like making so many auth checks kills the session as a security measure or something.</p>
  
  <p>Anyone mess around with letting a user know if their session has expired? Reason being is the application is very form based and users may leave a form page open and come back in the next day and the session has expired but they will go through the process of filling out the lengthy form and submit it just to find out they are not logged in.</p>
  
  <p>Any ideas or better ways to do this would be quite welcome.</p>