[FIX] 解決手機重複登入日誌問題並新增裝置詳細資訊偵測
All checks were successful
star-cloud-deploy-demo / deploy-demo (push) Successful in 59s

This commit is contained in:
2026-03-13 10:21:43 +08:00
parent 6fab048461
commit bb5d212569
7 changed files with 277 additions and 11 deletions

View File

@@ -34,10 +34,36 @@ class LogSuccessfulLogin
*/
public function handle(Login $event)
{
$ip = $this->request->ip();
$userAgent = $this->request->userAgent();
// 防重覆機制 (Debouncing): 10 秒內同使用者、同 IP 的記錄視為重複
$recentLog = UserLoginLog::where('user_id', $event->user->id)
->where('ip_address', $ip)
->where('login_at', '>=', now()->subSeconds(10))
->first();
if ($recentLog) {
return;
}
$agent = new \Jenssegers\Agent\Agent();
$agent->setUserAgent($userAgent);
$deviceType = 'desktop';
if ($agent->isTablet()) {
$deviceType = 'tablet';
} elseif ($agent->isMobile()) {
$deviceType = 'mobile';
}
UserLoginLog::create([
'user_id' => $event->user->id,
'ip_address' => $this->request->ip(),
'user_agent' => $this->request->userAgent(),
'ip_address' => $ip,
'user_agent' => $userAgent,
'device_type' => $deviceType,
'browser' => $agent->browser(),
'platform' => $agent->platform(),
'login_at' => now(),
]);
}

View File

@@ -13,6 +13,9 @@ class UserLoginLog extends Model
'user_id',
'ip_address',
'user_agent',
'device_type',
'browser',
'platform',
'login_at',
];

View File

@@ -25,9 +25,6 @@ class AppServiceProvider extends ServiceProvider
if (!$this->app->isLocal()) {
\Illuminate\Support\Facades\URL::forceScheme('https');
}
// 記錄使用者成功登入的歷史
Event::listen(Login::class, LogSuccessfulLogin::class);
}
}