Optimized expired session token cleanup

This commit is contained in:
Bernd Bestel 2026-08-28 17:25:55 +02:00
parent c9d6c02660
commit 230f1918b5
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
3 changed files with 4 additions and 5 deletions

View File

@ -25,7 +25,7 @@ class SessionAuthMiddleware extends BaseAuthMiddleware
$user = $sessionService->GetUserByToken(SessionService::SESSION_TOKEN_TYPE_REMEMBER_ME, $_COOKIE[SessionService::SESSION_TOKEN_COOKIE_NAME_REMEMBER_ME]);
if ($user !== null)
{
// Remember me token was valid => create a new access token and extend the lifetime of the remember me token cookie
// Remember me token is valid => create a new access token and extend the lifetime of the remember me token cookie
$token = $sessionService->CreateToken(SessionService::SESSION_TOKEN_TYPE_ACCESS, $user->id, GetClientUserAgent());
self::SetSessionCookie(SessionService::SESSION_TOKEN_TYPE_ACCESS, $token);
self::SetSessionCookie(SessionService::SESSION_TOKEN_TYPE_REMEMBER_ME, $_COOKIE[SessionService::SESSION_TOKEN_COOKIE_NAME_REMEMBER_ME]);
@ -33,7 +33,7 @@ class SessionAuthMiddleware extends BaseAuthMiddleware
}
}
// Remember me token was also not valid => remove the cookie
// Remember me token is also not valid => remove the cookie
self::RemoveSessionCookie(SessionService::SESSION_TOKEN_TYPE_REMEMBER_ME);
}

View File

@ -40,8 +40,6 @@ class DatabaseMigrationService extends BaseService
{
DatabaseService::GetInstance()->ExecuteDbStatement('VACUUM');
}
SessionService::GetInstance()->DeleteExpiredTokens();
}
private function ExecutePhpMigrationWhenNeeded(int $migrationId, string $phpFile, int &$migrationCounter)

View File

@ -45,6 +45,7 @@ class SessionService extends BaseService
public function DeleteToken(int $tokenType, string $token)
{
$this->DB->sessions()->where('token_type = :1 AND token_hash = :2', $tokenType, hash('sha256', $token))->delete();
$this->DeleteExpiredTokens();
}
public function ValidateToken(int $tokenType, string $token)
@ -89,7 +90,7 @@ class SessionService extends BaseService
return $this->DB->users()->orderBy('id')->limit(1)->fetch();
}
public function DeleteExpiredTokens()
private function DeleteExpiredTokens()
{
$this->DB->sessions()->where('expires < :1', date('Y-m-d H:i:s', time()))->delete();
}