Detect risky logins in Laravel
Add configurable risk scoring, alerts, and login blocking to a Laravel application.
A valid password is not always enough
A correct password does not prove that the person signing in is the legitimate account owner. The session may have been stolen, the password may have come from a breach, or the login context may simply look wrong compared with that user’s history. On a self-hosted application, where I am usually the person handling the alerts too, I want a stronger signal than a forgotten line in an application log.
Laravel Suspicious Login by Spatie adds that layer without replacing Laravel authentication. It records login attempts and evaluates successful sign-ins using signals such as the device, IP address, location, and account history. The result is a risk score that can trigger a user notification, block the login, or run custom logic.
The package listens to Laravel authentication events, so it works with Breeze, Jetstream, Fortify, and custom session controllers. After installing it and running its installer, it publishes configuration and a migration for the login_attempts table. It also enables DefaultPreset, which evaluates logins and notifies on medium and high risk without blocking by default.
composer require spatie/laravel-suspicious-login
php artisan suspicious-login:installRisk is built from several signals
The key point is that a single signal is rarely enough to make a reliable decision. A new IP can mean a new home connection, while a new country can mean the user is travelling. The package adds up the scores from checks that flag unusual activity and assigns a risk level using configured thresholds.
There are checks for new devices, new IP addresses, new countries, impossible travel, unusual times, failed attempts, and IP reputation. I would not enable everything just because it is available, since every application has a different access pattern. On an internal admin panel, for example, allowed countries and impossible travel may matter far more than changing IP addresses.
Presets define three things together: which checks run, how much each check contributes, and what happens at each risk level. The package includes DefaultPreset, MonitorOnlyPreset, and NonePreset. For an existing production application, I would start with MonitorOnlyPreset, since it records and evaluates activity without notifying or blocking anyone.
// config/suspicious-login.php
'preset' => Spatie\SuspiciousLogin\Presets\MonitorOnlyPreset::class,A preset for a sensitive admin area
Once I understand what normal access looks like, I prefer an explicit preset for sensitive accounts. In this example, a new device increases the score, impossible travel carries significant weight, and the admin area only allows logins from Spain, Portugal, and France. These values are not universal: they need to match the users, the application, and the cost of blocking a legitimate login.
namespace App\SuspiciousLogin;
use Spatie\SuspiciousLogin\Actions\Action;
use Spatie\SuspiciousLogin\Actions\BlockLoginAction;
use Spatie\SuspiciousLogin\Actions\NotifyUserAction;
use Spatie\SuspiciousLogin\Checks\CountryCheck;
use Spatie\SuspiciousLogin\Checks\ImpossibleTravelCheck;
use Spatie\SuspiciousLogin\Checks\NewDeviceCheck;
use Spatie\SuspiciousLogin\Presets\Preset;
class AdminPreset extends Preset
{
protected int $mediumRiskThreshold = 40;
protected int $highRiskThreshold = 70;
public function checks(): array
{
return [
NewDeviceCheck::new()->score(25),
ImpossibleTravelCheck::new()
->maxSpeedInKilometersPerHour(1000)
->score(60),
CountryCheck::new()->onlyAllow(['ES', 'PT', 'FR'])->score(60),
];
}
public function onMediumRisk(): Action|array
{
return NotifyUserAction::new();
}
public function onHighRisk(): Action|array
{
return [NotifyUserAction::new(), BlockLoginAction::new()];
}
}Action order matters here: BlockLoginAction must be last because it stops the login request. With this preset, medium-risk logins notify the user, while high-risk logins notify and block. A preset can also be selected per attempt with SuspiciousLogin::determinePresetUsing(), which is useful when administrators need stricter rules than ordinary users.
The fine print before blocking users
This is not a second factor and it does not replace strong passwords, rate limiting, safe account recovery, or MFA. It assesses the login after Laravel has already authenticated the user successfully. If I need an additional verification step, I still need to build a custom action that fits the authentication flow in my application.
IP geolocation is not an exact physical location source either. The impossible-travel check needs a driver that provides coordinates, and the package ignores short distances by default because geolocation can be inaccurate. VPNs, corporate networks, and mobile IP addresses can all produce suspicious-looking results without indicating an attack.
Device identification needs the same careful treatment. CookieFingerprinter uses a cookie to distinguish browsers, but clearing cookies, private browsing, or switching browsers will make the next login look like a new device. The login route must use Laravel’s web middleware group, and trusted proxies need to be configured correctly when TLS ends at a proxy.
That is why I would not enable aggressive blocking on day one. I would observe real traffic with MonitorOnlyPreset, review stored attempts, and then apply different rules to administrators, staff, and regular users. The package adds context and automation, but the actual security policy remains the responsibility of the team maintaining the application.
Source: Laravel News Official documentation: Spatie Laravel Suspicious Login