Session: immutable holder of token, owning User, createdAt, expiresAt. isExpired(now) compares >= expiresAt. SessionModel keys on token (string primary, non-incrementing). migration adds sessions table with foreign user_id (cascade on user delete) and indexed expires_at for cleanup queries. EloquentSessionRepository takes UserRepository to rehydrate the owning User on findByToken; sessions for deleted users return null. FakeSessionRepository mirrors with an in-memory map keyed by token, defensive copies on read.
44 lines
904 B
PHP
44 lines
904 B
PHP
<?php
|
|
|
|
namespace App\Auth;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @property string $token
|
|
* @property int $user_id
|
|
* @property Carbon $created_at
|
|
* @property Carbon $expires_at
|
|
*
|
|
* @method static Builder<static>|SessionModel newModelQuery()
|
|
* @method static Builder<static>|SessionModel newQuery()
|
|
* @method static Builder<static>|SessionModel query()
|
|
*
|
|
* @mixin \Eloquent
|
|
*/
|
|
class SessionModel extends Model
|
|
{
|
|
protected $table = 'sessions';
|
|
|
|
protected $primaryKey = 'token';
|
|
|
|
public $incrementing = false;
|
|
|
|
protected $keyType = 'string';
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = [
|
|
'token',
|
|
'user_id',
|
|
'created_at',
|
|
'expires_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'created_at' => 'datetime',
|
|
'expires_at' => 'datetime',
|
|
];
|
|
}
|