Files
sam-api/app/Models/LoginToken.php

90 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
use App\Models\Members\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
/**
* MNG DEV 자동 로그인용 One-Time Token 모델
*/
class LoginToken extends Model
{
protected $fillable = [
'user_id',
'token',
'expires_at',
];
protected $casts = [
'expires_at' => '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();
}
}