4

I am using the default laravel 5.2 authorization library.

I am using the remember me functionality with a 24 hour time out.

I need to log user_id with IP address in an audit table after each user visit to the webste.

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.

protected $listen = [
    'Illuminate\Auth\Events\Login' => [
        'App\Listeners\LogSuccessfulLogin',
    ],

This works for all logins where the login screen is used with username and password supplied by the user.

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.

In the API documentation (https://laravel.com/api/5.2/Illuminate/Auth/Events.html) there is no event like for example 'LogInViaCookie'.

I have tried adding a method to AuthController-

/**
 * Add audit.
 *
 * @param  $request
 * @param  $user
 */
protected function authenticated(Request $request, $user)
{
    try
    {
        $audit = Audit::create(['internet_protocol' => $request->ip,
                'uid' => $user->id,
                'general' => 'viaCookie']);
        $audit->save();
    }catch(\Exception $e){
        Log::error($e->getMessage());
    }
}

This authenticated method, as I understand it, should be fired from the AuthenticatesUsers trait (line 115)-

    if (method_exists($this, 'authenticated')) {
        return $this->authenticated($request, Auth::guard($this->getGuard())->user());
    }

However, it appears this method is not fired when logging in via cookie.

How can I listen for and capture this type of 'LogInViaCookie' event to update my audit table?

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.

This is my viaCookie.php-

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);
}
}

And my Kernel.php update-

protected $middlewareGroups = [
    'web' => [
        \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,
    ],

Am I missing something? dd isn't being fired.

8
  • I must be dumb, I can't understand what you're saying about login via cookie. When the user logged in it will be expired in 24 hours so if user access your app before 24 hours passes there is no need to login anymore and I believe there is no single method in AuthController will be executed when user still logged in.
    – Rifki
    Commented May 30, 2016 at 16:16
  • Thanks for your reply. Sorry I'm probably not explaining very well. I've done some more reading around the 5.2 authentication documentation. It appears there is now a Auth::viaRemember() method (laravel.com/docs/5.2/authentication#remembering-users) which tells you if a user was authenticated via the remember me option. So, where could I call it in order to ensure that which ever route the user goes down (apart from the login view which is handled above) am able to log the event to my audit table? Commented May 30, 2016 at 18:05
  • If you look at login() method in Illuminate\Auth\SessionGuard there is $this->fireLoginEvent($user, $remember); which triggered Illuminate\Auth\Events\Login, could be the problem is when you handle the event?
    – Rifki
    Commented May 30, 2016 at 18:35
  • Can you show me you event listener?
    – Rifki
    Commented May 30, 2016 at 18:46
  • Try to use a middleware lets says viaCookie, in the handle() method before return $next($request); do a check if(Auth::viaRemember()) followed by code to write the audit. Then apply the middleware to routes.
    – Rifki
    Commented May 30, 2016 at 19:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.