title: Laravel 5.2 - event handling - log a user login via remember me
body: <p>I am using the default laravel 5.2 authorization library.</p>
  
  <p>I am using the remember me functionality with a 24 hour time out.</p>
  
  <p>I need to log user_id with IP address in an audit table after each user visit to the webste.</p>
  
  <p>I have done this using the EventServiceProvider, listening for the login event
   and then using the request object to identify the IP and persisting to the database.</p>
  
  <pre><code>protected $listen = [
      'Illuminate\Auth\Events\Login' =&gt; [
          'App\Listeners\LogSuccessfulLogin',
      ],
  </code></pre>
  
  <p>This works for all logins where the login screen is used with username and password supplied by the user.</p>
  
  <p>However if the user accesses the website via the session cookie (ie- login within 24 hours of the previous login) the login is not recorded. So it must follow a different path within the authorization library.</p>
  
  <p>In the API documentation (<a href="https://laravel.com/api/5.2/Illuminate/Auth/Events.html" rel="nofollow">https://laravel.com/api/5.2/Illuminate/Auth/Events.html</a>) there is no event like for example 'LogInViaCookie'.</p>
  
  <p>I have tried adding a method to AuthController-</p>
  
  <pre><code>/**
   * Add audit.
   *
   * @param  $request
   * @param  $user
   */
  protected function authenticated(Request $request, $user)
  {
      try
      {
          $audit = Audit::create(['internet_protocol' =&gt; $request-&gt;ip,
                  'uid' =&gt; $user-&gt;id,
                  'general' =&gt; 'viaCookie']);
          $audit-&gt;save();
      }catch(\Exception $e){
          Log::error($e-&gt;getMessage());
      }
  }
  </code></pre>
  
  <p>This authenticated method, as I understand it, should be fired from the AuthenticatesUsers trait (line 115)-</p>
  
  <pre><code>    if (method_exists($this, 'authenticated')) {
          return $this-&gt;authenticated($request, Auth::guard($this-&gt;getGuard())-&gt;user());
      }
  </code></pre>
  
  <p>However, it appears this method is not fired when logging in via cookie.</p>
  
  <p>How can I listen for and capture this type of 'LogInViaCookie' event to update my audit table?</p>
  
  <p>UPDATE 30/05/16 21:21
  Rifki- That sounds like a good solution.  I've tried implementing but I can't get it to work.  </p>
  
  <p>This is my viaCookie.php-</p>
  
  <pre><code>namespace App\Http\Middleware;
  use Closure;
  use Illuminate\Support\Facades\Auth;
  
  class viaCookie
  {
  /**
   * Handle an incoming request.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \Closure  $next
   * @return mixed
   */
  public function handle($request, Closure $next)
  {
  if(Auth::viaRemember()) {
      dd('logged in via cookie');
  }
      return $next($request);
  }
  }
  </code></pre>
  
  <p>And my Kernel.php update-</p>
  
  <pre><code>protected $middlewareGroups = [
      'web' =&gt; [
          \App\Http\Middleware\EncryptCookies::class,
          \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
          \Illuminate\Session\Middleware\StartSession::class,
          \Illuminate\View\Middleware\ShareErrorsFromSession::class,
          \App\Http\Middleware\VerifyCsrfToken::class,
          \App\Http\Middleware\viaCookie::class,
      ],
  </code></pre>
  
  <p>Am I missing something? dd isn't being fired.</p>