From 5fee418e3dddbc83a1946b7f6cc697a63968faf1 Mon Sep 17 00:00:00 2001 From: Axel Date: Fri, 30 Jun 2023 10:03:11 +0200 Subject: [PATCH 1/2] Make configurable the number of minutes after which the user is no longer considered a new user. --- config/authentication-log.php | 3 +++ src/Listeners/LoginListener.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/config/authentication-log.php b/config/authentication-log.php index 178e537..1570f63 100644 --- a/config/authentication-log.php +++ b/config/authentication-log.php @@ -26,6 +26,9 @@ // The Notification class to send 'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\NewDevice::class, + + // Number of minutes after which the user is no longer considered as a new user + 'new_user_in_minutes' => env('NEW_USER_IN_MINUTES', 1), ], 'failed-login' => [ // Send the FailedLogin notification diff --git a/src/Listeners/LoginListener.php b/src/Listeners/LoginListener.php index 7c0eed5..8daaae3 100644 --- a/src/Listeners/LoginListener.php +++ b/src/Listeners/LoginListener.php @@ -28,7 +28,7 @@ public function handle($event): void $ip = $this->request->ip(); $userAgent = $this->request->userAgent(); $known = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->whereLoginSuccessful(true)->first(); - $newUser = Carbon::parse($user->{$user->getCreatedAtColumn()})->diffInMinutes(Carbon::now()) < 1; + $newUser = Carbon::parse($user->{$user->getCreatedAtColumn()})->diffInMinutes(Carbon::now()) < config('authentication-log.notifications.new-device.new_user_in_minutes', 1); $log = $user->authentications()->create([ 'ip_address' => $ip, From b5f0c14f3028f3f675cdd8e09a56b222555de15a Mon Sep 17 00:00:00 2001 From: Axel Date: Fri, 30 Jun 2023 10:19:36 +0200 Subject: [PATCH 2/2] Update LoginListener.php Small optimisation. --- src/Listeners/LoginListener.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Listeners/LoginListener.php b/src/Listeners/LoginListener.php index 8daaae3..29d345d 100644 --- a/src/Listeners/LoginListener.php +++ b/src/Listeners/LoginListener.php @@ -27,8 +27,6 @@ public function handle($event): void $user = $event->user; $ip = $this->request->ip(); $userAgent = $this->request->userAgent(); - $known = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->whereLoginSuccessful(true)->first(); - $newUser = Carbon::parse($user->{$user->getCreatedAtColumn()})->diffInMinutes(Carbon::now()) < config('authentication-log.notifications.new-device.new_user_in_minutes', 1); $log = $user->authentications()->create([ 'ip_address' => $ip, @@ -38,7 +36,14 @@ public function handle($event): void 'location' => config('authentication-log.notifications.new-device.location') ? optional(geoip()->getLocation($ip))->toArray() : null, ]); - if (! $known && ! $newUser && config('authentication-log.notifications.new-device.enabled')) { + if (empty(config('authentication-log.notifications.new-device.enabled'))) { + return; + } + + $known = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->whereLoginSuccessful(true)->first(); + $newUser = Carbon::parse($user->{$user->getCreatedAtColumn()})->diffInMinutes(Carbon::now()) < config('authentication-log.notifications.new-device.new_user_in_minutes', 1); + + if (! $known && ! $newUser) { $newDevice = config('authentication-log.notifications.new-device.template') ?? NewDevice::class; $user->notify(new $newDevice($log)); }