Skip to content

Commit

Permalink
Support session database
Browse files Browse the repository at this point in the history
  • Loading branch information
jelib3an committed Jul 7, 2020
1 parent 34eb315 commit 68c5453
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ When the session is about to expire, show a dialog so the user can choose not to

![](screenshot.png)

## How it works

This code doesn't actually log the user out. It still relies on Laravel's mechanism for expiring sessions. All it does is periodically check how much time before the session will expire and alert the user when it is almost time.

## Installation

Laravel package and NPM dependencies
Expand Down
18 changes: 18 additions & 0 deletions src/TimeoutCalculator/DatabaseSessionChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Vectorwyse\IdleTimeoutAlert\TimeoutCalculator;

use Illuminate\Support\Facades\DB;

class DatabaseSessionChecker extends SessionChecker
{
public function getLastModified()
{
$session = DB::table('sessions')->where('id', $this->sessionId)->first();
if (!$session) {
throw new TimeoutCalculatorException('Session not found');
}

return $session->last_activity;
}
}
5 changes: 3 additions & 2 deletions src/TimeoutCalculator/TimeoutCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public static function getSecondsLeft(Request $request)
$sessionId = Crypt::decryptString($request->cookie('laravel_session'));

switch (config('session.driver')) {
case 'file': $checker = new FileSessionChecker($sessionId); break;
default: throw new TimeoutCalculatorException('Session driver not supported');
case 'database': $checker = new DatabaseSessionChecker($sessionId); break;
case 'file': $checker = new FileSessionChecker($sessionId); break;
default: throw new TimeoutCalculatorException('Session driver not supported');
}

$secondsSince = time() - $checker->getLastModified();
Expand Down

0 comments on commit 68c5453

Please sign in to comment.