Skip to content

Commit

Permalink
fix: make tokens unique
Browse files Browse the repository at this point in the history
  • Loading branch information
Ion Babin committed Apr 28, 2024
1 parent e359ad2 commit 8587883
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 10 additions & 1 deletion app/Models/LoginToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down

0 comments on commit 8587883

Please sign in to comment.