Skip to content

Commit a3ea677

Browse files
authored
Merge pull request #392 from pjsde/refactor_recordActiveDate
Refactor record active date
2 parents 80e2fe8 + 0451ab5 commit a3ea677

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,6 @@ public function recordActiveDate(): void
237237

238238
$this->user->last_active = Time::now();
239239

240-
$this->provider->save($this->user);
240+
$this->provider->updateActiveDate($this->user);
241241
}
242242
}

src/Authentication/Authenticators/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public function recordActiveDate(): void
830830

831831
$this->user->last_active = Time::now();
832832

833-
$this->provider->save($this->user);
833+
$this->provider->updateActiveDate($this->user);
834834
}
835835

836836
/**

src/Models/UserModel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,14 @@ protected function saveEmailIdentity(array $data): array
320320

321321
return $data;
322322
}
323+
324+
/**
325+
* Updates the user's last active date.
326+
*/
327+
public function updateActiveDate(User $user): void
328+
{
329+
$this->builder->set('last_active', $user->last_active)
330+
->where('id', $user->id)
331+
->update();
332+
}
323333
}

tests/Authentication/Filters/SessionFilterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,16 @@ public function testFilterSuccess(): void
4545
// Last Active should have been updated
4646
$this->assertNotEmpty(auth('session')->user()->last_active);
4747
}
48+
49+
public function testRecordActiveDate(): void
50+
{
51+
$user = fake(UserModel::class);
52+
$_SESSION['user']['id'] = $user->id;
53+
54+
$this->withSession(['user' => ['id' => $user->id]])
55+
->get('protected-route');
56+
57+
// Last Active should be greater than 'updated_at' column
58+
$this->assertGreaterThan(auth('session')->user()->updated_at, auth('session')->user()->last_active);
59+
}
4860
}

tests/Authentication/Filters/TokenFilterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,17 @@ public function testFilterSuccess(): void
5050
$this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken());
5151
$this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id);
5252
}
53+
54+
public function testRecordActiveDate(): void
55+
{
56+
/** @var User $user */
57+
$user = fake(UserModel::class);
58+
$token = $user->generateAccessToken('foo');
59+
60+
$this->withHeaders(['Authorization' => 'Bearer ' . $token->raw_token])
61+
->get('protected-route');
62+
63+
// Last Active should be greater than 'updated_at' column
64+
$this->assertGreaterThan(auth('tokens')->user()->updated_at, auth('tokens')->user()->last_active);
65+
}
5366
}

0 commit comments

Comments
 (0)