From 13b80620813106b97043e7e130abd6011d25d76a Mon Sep 17 00:00:00 2001 From: Julio Motol Date: Thu, 11 May 2023 00:32:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Should=20properly=20initialize?= =?UTF-8?q?=20auth=20timeout=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julio Motol --- CHANGELOG.md | 6 ++++++ src/Listeners/InitializeAuthTimeout.php | 2 +- tests/CheckAuthTimeoutTest.php | 18 ++++++++++-------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 060c6c9..800adb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v4.0.1 (2022-05-11) + +### Fixed + +- Should properly initialize auth timeout session. + ## v4.0.0 (2022-02-16) Laravel Auth Timeout has received a complete refresh. Please make sure to read through all the changes. diff --git a/src/Listeners/InitializeAuthTimeout.php b/src/Listeners/InitializeAuthTimeout.php index 5f2d278..e680670 100644 --- a/src/Listeners/InitializeAuthTimeout.php +++ b/src/Listeners/InitializeAuthTimeout.php @@ -9,7 +9,7 @@ class InitializeAuthTimeout { public function handle(): void { - if (! Session::isStarted()) { + if (Session::isStarted()) { AuthTimeout::init(); } } diff --git a/tests/CheckAuthTimeoutTest.php b/tests/CheckAuthTimeoutTest.php index 412595a..d2c16e9 100644 --- a/tests/CheckAuthTimeoutTest.php +++ b/tests/CheckAuthTimeoutTest.php @@ -4,15 +4,12 @@ use Carbon\Carbon; use Illuminate\Auth\AuthenticationException; -use Illuminate\Auth\Events\Login; use Illuminate\Foundation\Auth\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Session; use JulioMotol\AuthTimeout\Facades\AuthTimeout; use JulioMotol\AuthTimeout\Middlewares\CheckAuthTimeout; -use function Pest\Laravel\actingAs; use function Pest\Laravel\travel; afterEach(function () { @@ -22,7 +19,11 @@ }); it('should proceed when user is guest', function () { + expect(AuthTimeout::lastActiveAt())->toBeNull(); + runMiddleware(); + + expect(AuthTimeout::lastActiveAt())->toBeNull(); }); it('should timeout when idled', function () { @@ -53,10 +54,10 @@ })->throws(AuthenticationException::class); it('should reset session when not timed out', function () { - $startTime = Carbon::now(); - login(); + $startTime = AuthTimeout::lastActiveAt(); + travel(config('auth-timeout.timeout') - 1)->minutes(); runMiddleware(); @@ -66,10 +67,11 @@ function login() { - $user = new User(); + Session::start(); + + Auth::login(new User()); - actingAs($user); - Event::dispatch(new Login('test', $user, false)); + expect(AuthTimeout::lastActiveAt())->toBeInstanceOf(Carbon::class); } function runMiddleware()