feat: MNG → DEV 자동 로그인 API 구현
- login_tokens 테이블 마이그레이션 생성 - LoginToken 모델 생성 (One-Time Token 관리) - POST /api/v1/token-login 엔드포인트 추가 - 토큰 검증 후 access_token 발급, 1회용 토큰 삭제 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
89
app/Models/LoginToken.php
Normal file
89
app/Models/LoginToken.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user