Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/authentication-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/Listeners/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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));
}
Expand Down