From 7f4583ff622f325880412af233e88bd4d326c79a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Azad=20Furkan=20=C5=9EAKAR?= Date: Sun, 24 Mar 2024 23:55:48 +0300 Subject: [PATCH] Update code for OTP login functionality --- .phpunit.cache/test-results | 2 +- .../create_filament_otp_login_table.php.stub | 2 +- src/Filament/Pages/Login.php | 4 ++-- src/Models/OtpCode.php | 16 ++++++++++------ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.phpunit.cache/test-results b/.phpunit.cache/test-results index 2036704..189a585 100644 --- a/.phpunit.cache/test-results +++ b/.phpunit.cache/test-results @@ -1 +1 @@ -{"version":"pest_2.34.5","defects":[],"times":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.159,"P\\Tests\\ExampleTest::__pest_evaluable_it_can_test":0.002}} \ No newline at end of file +{"version":"pest_2.34.5","defects":[],"times":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":0.154,"P\\Tests\\ExampleTest::__pest_evaluable_it_can_test":0.008}} \ No newline at end of file diff --git a/database/migrations/create_filament_otp_login_table.php.stub b/database/migrations/create_filament_otp_login_table.php.stub index da7554f..52242d1 100644 --- a/database/migrations/create_filament_otp_login_table.php.stub +++ b/database/migrations/create_filament_otp_login_table.php.stub @@ -10,7 +10,7 @@ return new class extends Migration { $table_name = config('filament-otp-login.table_name'); - Schema::create($table_name, function (Blueprint $table) use ($default_column) { + Schema::create($table_name, function (Blueprint $table) { $table->id(); $table->string('code'); $table->string('email'); diff --git a/src/Filament/Pages/Login.php b/src/Filament/Pages/Login.php index a1b7a3f..7993242 100644 --- a/src/Filament/Pages/Login.php +++ b/src/Filament/Pages/Login.php @@ -107,7 +107,7 @@ protected function doLogin(): void public function verifyCode(): void { - $code = OtpCode::where('code', $this->data['otp'])->first(); + $code = OtpCode::whereCode($this->data['otp'])->first(); if (! $code) { throw ValidationException::withMessages([ @@ -130,7 +130,7 @@ public function generateCode(): void $length = config('filament-otp-login.otp_code.length'); $code = str_pad(rand(0, 10 ** $length - 1), $length, '0', STR_PAD_LEFT); - } while (OtpCode::where('code', $code)->exists()); + } while (OtpCode::whereCode($code)->exists()); $this->otpCode = $code; diff --git a/src/Models/OtpCode.php b/src/Models/OtpCode.php index 14b374f..90674a8 100644 --- a/src/Models/OtpCode.php +++ b/src/Models/OtpCode.php @@ -2,7 +2,9 @@ namespace Afsakar\FilamentOtpLogin\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\MassPrunable; /** * @property string $code @@ -11,24 +13,26 @@ */ class OtpCode extends Model { + use MassPrunable; protected $guarded = []; protected $casts = [ 'expires_at' => 'datetime', ]; - public function isValid() + public function __construct(array $attributes = []) { - return $this->expires_at->isFuture(); + parent::__construct($attributes); + $this->setTable(config('filament-otp-login.table_name')); } - public function scopeValid($query) + public function prunable(): Builder { - return $query->where('expires_at', '>', now()); + return static::where('expires_at', '<=', now()->subDay()->startOfDay()); } - public function scopeCode($query, $code) + public function isValid(): bool { - return $query->where('code', $code); + return $this->expires_at->isFuture(); } }