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..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()) < 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)); }