Skip to content

Commit

Permalink
Update code for OTP login functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
afsakar committed Mar 24, 2024
1 parent b852755 commit 7f4583f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/Filament/Pages/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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;

Expand Down
16 changes: 10 additions & 6 deletions src/Models/OtpCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}

0 comments on commit 7f4583f

Please sign in to comment.