2025-12-20 13:43:13 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* DEV 사이트 자동 로그인 URL 생성
|
2025-12-22 19:51:36 +09:00
|
|
|
* 현재 도메인에서 'mng'를 'dev'로 변경하여 동적 생성
|
2025-12-20 13:43:13 +09:00
|
|
|
*/
|
|
|
|
|
public function getAutoLoginUrl(): string
|
|
|
|
|
{
|
2025-12-22 19:51:36 +09:00
|
|
|
$currentHost = request()->getHost(); // mng.sam.kr 또는 mng.codebridge-x.com
|
|
|
|
|
$devHost = str_replace('mng.', 'dev.', $currentHost);
|
|
|
|
|
$scheme = request()->getScheme(); // http 또는 https
|
2025-12-20 13:43:13 +09:00
|
|
|
|
2025-12-22 19:51:36 +09:00
|
|
|
return "{$scheme}://{$devHost}/auto-login?token={$this->token}";
|
2025-12-20 13:43:13 +09:00
|
|
|
}
|
|
|
|
|
}
|