I have a table called admission
and in this table I have two columns to indicate record locking. locked_by
that contains the user id when locked and locked_at
which holds the timestamp for when the record was locked.
Last one is probably not applicable here.
Okay so when my session expires, I would like to release the record that is locked by THIS user.
This is how I aproached it.
API
Route::group(['middleware' => ['auth:sanctum', 'updateLastActive']], function () {
updateLastActive.php
class UpdateLastActive
{
/**
* Handle an incoming request.
*
* @param Closure(IlluminateHttpRequest): (SymfonyComponentHttpFoundationResponse) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (Auth::check()) {
$user = Auth::user();
$sessionLifetime = config('session.lifetime'); // in minutes
$cutoffTime = Carbon::now()->subMinutes($sessionLifetime);
// Check if the user's session is expired
if ($user->last_active < $cutoffTime) {
// Unlock any records locked by this user
AppModelsAdmission::where('locked_by', $user->id)->update(['locked_by' => null]);
// Optionally log out the user
Auth::logout();
// Redirect to the login page or show a session expired message
return redirect()->route('login')->withErrors(['message' => 'Your session has expired due to inactivity.']);
}
// Update the last active timestamp if the session is still valid
$user->update(['last_active' => now()]);
}
return $next($request);
}
}
But it never releases the record. I get logged out on session expiry, but locked_by
never gets cleard.