Reputation: 620
I use Laravel 5.8
authentication and authorization.
I have an 'Admin' who manages Companies and Workers.
When Admin login i want to show what new companies and users was
created since last admin session. For that i thought to store in
database time when Admin's session finished and query from database
all Companies and Workers what was created since last session.
If Admin use logout
i can store time in database, but how to
store time if Admin just close tab ??
--------------*-
I created middleware to save time from last request:
public function handle($request, Closure $next)
{
if(Auth::check()){
$id = Auth::id();
Admin::whereId($id)->update(['last_enter' => now("Europe/Moscow")]);
}
return $next($request);
}
Upvotes: 2
Views: 446
Reputation: 4443
Since you want to archieve this only using laravel I would suggest you to check middlewares
. You can have a middleware where are requests will pass through so last request for you mean that that's the last time user has used it.
Here you can learn more about middleware in laravel.
Upvotes: 1
Reputation: 14550
Speaking anecdotally, you can use sendBeacon
with the unload
event to send a request. I had to use it to capture the amount of time a user spent watching a video and if they closed the tab I needed to record it, this method worked consistently for me.
window.addEventListener('unload', function() {
navigator.sendBeacon('your-url',
JSON.stringify({})
);
}, false);
Alternatively, you could in those instances where the user closes the window set the logout time to the session timeout time, for example: sign-in-time + session-timeout-time = logout-time
, but again, that isn't "accurate".
Upvotes: 1