'datetime', ]; /** * 토큰 만료 시간 (분) */ public const EXPIRES_IN_MINUTES = 5; /** * 사용자 관계 */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * 새 토큰 생성 */ public static function createForUser(int $userId): self { // 기존 토큰 삭제 (해당 사용자) self::where('user_id', $userId)->delete(); return self::create([ 'user_id' => $userId, 'token' => Str::random(64), 'expires_at' => now()->addMinutes(self::EXPIRES_IN_MINUTES), ]); } /** * 토큰으로 조회 및 검증 */ public static function findValidToken(string $token): ?self { return self::where('token', $token) ->where('expires_at', '>', now()) ->first(); } /** * 만료 여부 확인 */ public function isExpired(): bool { return $this->expires_at->isPast(); } /** * 토큰 사용 (1회용 - 사용 후 삭제) */ public function consume(): User { $user = $this->user; $this->delete(); return $user; } /** * 만료된 토큰 정리 */ public static function cleanupExpired(): int { return self::where('expires_at', '<', now())->delete(); } }