diff --git a/src/Session/DatabaseSessionHandler.php b/src/Session/DatabaseSessionHandler.php new file mode 100644 index 0000000..b161e7a --- /dev/null +++ b/src/Session/DatabaseSessionHandler.php @@ -0,0 +1,57 @@ + :timestamp to `last_active_at` => :datetime + * 2. Update all underlining `last_activity` usage to account for new column and format + * 3. Create `Session` model that has a virtual columns of `browser` and `location` that + * return mobiledetect parsing on `browser` and geoip data on `location` + * 4. Update `User` model to add a `sessions` relationship that returns all sessions + */ +class DatabaseSessionHandler extends \Illuminate\Session\DatabaseSessionHandler +{ + protected function expired($session): bool + { + if (isset($session->last_active_at)) { + return Carbon::parse($session->last_active_at)->lessThan(now()->subMinutes($this->minutes)); + } + + return false; + } + + protected function getDefaultPayload($data): array + { + $payload = [ + 'payload' => base64_encode($data), + 'last_active_at' => now(), + ]; + + if (!$this->container) { + return $payload; + } + + return tap($payload, function (&$payload) { + $this->addUserInformation($payload) + ->addRequestInformation($payload); + }); + } + + protected function performInsert($sessionId, $payload): bool|null + { + return parent::performInsert($sessionId, [ + ...$payload, + 'created_at' => now(), + ]); + } + + public function gc($lifetime): int + { + return $this->getQuery() + ->where('last_active_at', '<=', now()->subMinutes($lifetime)) + ->delete(); + } +} diff --git a/src/Session/ServiceProvider.php b/src/Session/ServiceProvider.php new file mode 100644 index 0000000..7d35cd7 --- /dev/null +++ b/src/Session/ServiceProvider.php @@ -0,0 +1,32 @@ +get('session.table'); + $lifetime = $app['config']->get('session.lifetime'); + $connection = $app['db']->connection($app['config']->get('session.connection')); + + return new DatabaseSessionHandler($connection, $table, $lifetime, $app); + }); + } +}