diff --git a/app/Models/LoginToken.php b/app/Models/LoginToken.php index cabfbb5..7736951 100644 --- a/app/Models/LoginToken.php +++ b/app/Models/LoginToken.php @@ -6,7 +6,6 @@ use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; - class LoginToken extends Model { use HasFactory; @@ -21,6 +20,16 @@ class LoginToken extends Model 'created' => RequestedLogin::class, ]; + public static function boot() + { + parent::boot(); + + static::creating(function($table) + { + $table->value = str()->random(40); + $table->valid_until = now()->addHours(config('constants.login_token_lifespan_days')); + }); + } public function createdToday(string $email): Collection { return $this->whereEmail($email)->whereCreatedAtDay(now()->day)->get(); diff --git a/database/migrations/2024_04_27_123107_create_login_tokens_table.php b/database/migrations/2024_04_27_123107_create_login_tokens_table.php index 0a4e6ef..2e4e468 100644 --- a/database/migrations/2024_04_27_123107_create_login_tokens_table.php +++ b/database/migrations/2024_04_27_123107_create_login_tokens_table.php @@ -4,15 +4,14 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class extends Migration { public function up(): void { Schema::create('login_tokens', function (Blueprint $table) { $table->id(); - $table->string('value')->default(str()->random(40)); + $table->string('value'); $table->string('email'); - $table->dateTime('valid_until')->default(now()->addHours(config('constants.login_token_lifespan_days'))); + $table->dateTime('valid_until'); $table->timestamps(); }); }