<?php

namespace App\Http\Middleware;

use Closure;
use App\Models;
class CheckCurrentlyLoggedInUsers
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if($request->session()->has('current_user')){
            $id = session('current_user.id');
            
            $device = $request->server('HTTP_USER_AGENT');
            $realm = $id . $device;
            $current_user = Models\LoggedUser::where('user_id', $id)->where('device', $device)->first();
            $current_user->last_seen_time = date('Y-m-d H:i:s');
            $current_user->ip_address = \Request::ip();

            $current_user->save();
        }
        return $next($request);
    }
}
