Detect suspicious logins in your Laravel app and decide how to respond
spatie/laravel-suspicious-login is a Laravel package for detect suspicious logins in your laravel app and decide how to respond.
It currently has 42 GitHub stars and 2.803 downloads on Packagist (latest version 1.0.0).
Install it with composer require spatie/laravel-suspicious-login.
Discover more Laravel packages by spatie
or browse all Laravel packages to compare alternatives.
Last updated
Imagine someone logs in to your account from the other side of the world. If you're travelling, that might be perfectly normal. But if you're sitting at home in Antwerp, it's suspicious. Someone might have gotten hold of your credentials.
We've built this package to notify users about suspicious logins and let you decide what happens next. It checks the device, IP address, location and login history to give each successful login a risk score. Depending on that score, it can email the user, block the login or run your own actions.
Here's how a login from a new laptop in New York, to an account that always logs in from Antwerp, is scored with DefaultPreset and geolocation configured:
Login from a new device +25
Login from a new IP address (5.6.7.8) +10
Login from a new country (US) +30
----
Risk score 65 → medium risk, the user gets an email
A preset lists the checks to run, how much each weighs, and which actions run at each risk level. It can also override the default risk thresholds from the config file. The package ships with DefaultPreset, MonitorOnlyPreset and NonePreset, and writing your own takes a few lines:
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 MyPreset extends Preset
{
public function checks(): array
{
return [
NewDeviceCheck::new()->score(30),
ImpossibleTravelCheck::new()->maxSpeedInKilometersPerHour(1000)->score(60),
CountryCheck::new()->onlyAllow(['BE', 'NL', 'FR'])->score(60),
];
}
public function onMediumRisk(): Action|array
{
return NotifyUserAction::new();
}
public function onHighRisk(): Action|array
{
return [NotifyUserAction::new(), BlockLoginAction::new()];
}
}
Register your preset in config/suspicious-login.php:
'preset' => App\SuspiciousLogin\MyPreset::class,
The default risk thresholds live in config/suspicious-login.php:
'risk_thresholds' => [
'medium' => 40,
'high' => 70,
],
Optionally define protected int $mediumRiskThreshold or protected int $highRiskThreshold in your preset to override either config value. DefaultPreset and MonitorOnlyPreset use the configured defaults.
To choose a preset per login, register a callback in a service provider's boot() method. For example, apply stricter checks to admins and skip assessment for exempt users:
use App\SuspiciousLogin\MyPreset;
use Spatie\SuspiciousLogin\Facades\SuspiciousLogin;
use Spatie\SuspiciousLogin\Models\LoginAttempt;
use Spatie\SuspiciousLogin\Presets\DefaultPreset;
use Spatie\SuspiciousLogin\Presets\NonePreset;
SuspiciousLogin::determinePresetUsing(function (LoginAttempt $attempt): string {
$user = $attempt->authenticatable;
if ($user->is_admin) {
return MyPreset::class;
}
if ($user->skip_login_checks) {
return NonePreset::class;
}
return DefaultPreset::class;
});
The user attributes above are application-specific. The callback receives the attempt before location lookups, with its user, guard and custom properties available. Return a preset class name, never null. NonePreset still records the login and fires LoginAttemptRecordedEvent, but skips location lookups, scoring and actions. Unlike MonitorOnlyPreset, it leaves the risk score and level unset. See choosing a preset per login.
Only the actions for the matching level run. List BlockLoginAction last because it logs the user out of their session guard and throws a validation exception.
An action is a small class with an execute method that receives the login attempt. There's no magic behind the ones the package ships with. Here's BlockLoginAction in full:
class BlockLoginAction extends Action
{
protected string $errorKey = 'email';
protected string $message = 'We blocked this login because it looked suspicious. Please contact support if this was you.';
public function errorKey(string $errorKey): static
{
$this->errorKey = $errorKey;
return $this;
}
public function message(string $message): static
{
$this->message = $message;
return $this;
}
public function execute(LoginAttempt $attempt): void
{
$attempt->update(['blocked' => true]);
event(new SuspiciousLoginBlockedEvent($attempt));
Auth::guard($attempt->guard)->logout();
$this->respond($attempt);
}
protected function respond(LoginAttempt $attempt): never
{
throw ValidationException::withMessages([
$this->errorKey => __($this->message),
]);
}
}
new() comes from the base Action class and resolves the action from the container, so actions can inject dependencies in their constructor. The fluent methods configure it in your preset, like BlockLoginAction::new()->message('...'). Because the login happens inside Auth::attempt(), the validation exception comes out of your login controller like any other validation error.
To change the response without replacing the blocking and logout behavior, extend BlockLoginAction and override protected function respond(LoginAttempt $attempt): never. See customizing the response.
Writing your own action, for example to ask for a second factor or alert your security team, works the same way: extend Action and implement execute. Checks are built the same way too: extend Check and return $this->pass() or $this->flag($reason) from evaluate.
You can also add MultipleCountriesCheck to your preset to flag logins from different countries within one hour, even when both countries are already known.
Checks, actions, geolocation and IP reputation drivers, device fingerprints and the model are all swappable, and the package fires events you can listen for.
Device fingerprints use an encrypted browser cookie by default, distinguishing browsers with identical user agents without JavaScript. Browser updates keep the same identity; clearing cookies creates a new one. Login routes need Laravel's web cookie middleware. For stateless apps, explicitly use the user-agent fingerprinter or your own identifier.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/laravel-suspicious-login
Run the install command:
php artisan suspicious-login:install
It publishes config/suspicious-login.php and the migration for the login_attempts table, then asks whether to run your application's pending migrations. If you decline, run php artisan migrate before using the package. It also offers to star the repository on GitHub.
That's it. The package listens for Laravel's Login and Failed auth events, so it works with Breeze, Jetstream, Fortify and your own login controllers without changes. Logins restored from a "remember me" cookie are not recorded.
Out of the box DefaultPreset is used, and the ipinfo driver looks up public IP addresses without requiring an API token. Lookups are cached for a week and subject to ipinfo's limits. You can choose another geolocation driver in config/suspicious-login.php, or set it to 'null' to disable lookups and avoid sending IP addresses to an external service.
The default mailable is sent to the user's email address and requires working mail configuration, not the Notifiable trait. It is queued, so run a queue worker unless you use the sync queue connection. Set mailable in config/suspicious-login.php to use your own mailable class.
You can publish the email's Markdown view to change its content or layout without replacing the mailable. Prefer Laravel notifications? Use a custom action.
For an existing application, consider starting with Spatie\SuspiciousLogin\Presets\MonitorOnlyPreset. It records scores and fires events without notifying or blocking users, so you can review the results before enabling actions.
All documentation is available on our documentation site.
The package ships guidelines and a detecting-suspicious-logins skill for Laravel Boost. Run php artisan boost:install in your application and select this package to give your coding agent its APIs, extension points, and usage examples. See the setup instructions.
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
Get paid by clients abroad
Wise gives you local bank details in several currencies and converts at the mid-market rate. Useful when your invoices are not in your own currency.
Try WiseNew Laravel packages worth a look, once a month. No spam, one-click unsubscribe.