{"slug": "detecting-suspicious-logins-in-your-laravel-app", "title": "★ Detecting suspicious logins in your Laravel app", "summary": "Spatie released laravel-suspicious-login, a Laravel package that scores login attempts against configurable risk thresholds and triggers actions such as emailing the user or blocking the login. The package's DefaultPreset assigns points per signal — +25 for a new device, +10 for a new IP address and +30 for a new country — with default thresholds of 40 for medium risk and 70 for high risk set in config/suspicious-login.php. The package also logs all successful and failed login attempts, scoring assessed logins while recording failed ones without a score, and ships a Laravel Boost skill so an AI coding agent can install and customize it.", "body_md": "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 your hometown, it's suspicious. Someone might have gotten hold of your credentials.\n\nWe've released [laravel-suspicious-login](https://spatie.be/docs/laravel-suspicious-login) to notify users about suspicious logins and let you decide what happens next. It looks at signals such as the browser, location and earlier failed attempts. You can email the user, block the login or run your own actions.\n\nYou can install it with Composer, or let your AI coding agent do it. The package includes a Laravel Boost skill for installing and customizing it.\n\nLet's take a look at how the package detects signals that might indicate a suspicious login.\n\nAn unfamiliar IP address isn't much of a reason to reject a login. Mobile networks change addresses all the time. Several signals together can be more useful.\n\nEach check adds a configurable number of points when it flags a login. For example, with `DefaultPreset`, working geolocation and an existing login history, these checks could contribute:\n\n```\nNew device       +25\nNew IP address   +10\nNew country      +30\n                 ---\nTotal             65\n```\n\nAssuming the other checks pass, that's medium risk. The default thresholds in `config/suspicious-login.php` are:\n\n``` js\n'risk_thresholds' => [\n    'medium' => 40,\n    'high' => 70,\n],\n```\n\nThe default preset also checks for [impossible travel](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/impossible-travel), [repeated failed attempts](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/failed-attempts), [credential stuffing](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/credential-stuffing) and [suspicious user agents](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/suspicious-user-agent).\n\nThe package also [logs all login attempts](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/working-with-login-attempts), both successful and failed, so you can review a user's login history. Assessed logins include their score and the reasons behind it. Failed logins are [recorded, but aren't scored](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/how-a-login-is-scored).\n\nA preset class combines the checks, thresholds and actions in one PHP class. Here's a small example for a session-based application. A new country should trigger an email. A new country together with a new device should also block the login.\n\nLet's look at this example preset implementation:\n\n``` php\nnamespace App\\SuspiciousLogin;\n\nuse Spatie\\SuspiciousLogin\\Actions\\Action;\nuse Spatie\\SuspiciousLogin\\Actions\\BlockLoginAction;\nuse Spatie\\SuspiciousLogin\\Actions\\NotifyUserAction;\nuse Spatie\\SuspiciousLogin\\Checks\\NewCountryCheck;\nuse Spatie\\SuspiciousLogin\\Checks\\NewDeviceCheck;\nuse Spatie\\SuspiciousLogin\\Presets\\Preset;\n\nclass AdminPreset extends Preset\n{\n    protected int $mediumRiskThreshold = 40;\n\n    protected int $highRiskThreshold = 70;\n\n    public function checks(): array\n    {\n        return [\n            NewDeviceCheck::new()->score(30),\n            NewCountryCheck::new()->score(40),\n        ];\n    }\n\n    public function onMediumRisk(): Action|array\n    {\n        return NotifyUserAction::new();\n    }\n\n    public function onHighRisk(): Action|array\n    {\n        return [\n            NotifyUserAction::new(),\n            BlockLoginAction::new(),\n        ];\n    }\n}\n```\n\nDon't forget to specify your preset class in the config file:\n\n``` php\nuse App\\SuspiciousLogin\\AdminPreset;\n\n'preset' => AdminPreset::class,\n```\n\nA new device adds 30 points, so nothing happens yet. A new country adds 40 points and sends an email. If both checks flag the login, the score reaches 70. The user gets an email and the login is blocked. Leave out the threshold properties if you want to use the values from your config.\n\nA high-risk login only runs `onHighRisk()`, not `onMediumRisk()`. That's why the email action appears in both methods. Put `BlockLoginAction` last, because it logs the user out and stops the request. It works with session-based logins; other authentication flows need their own blocking action.\n\nThe device and country checks compare against earlier successful logins that weren't blocked, so they won't flag someone's first login. The country check also needs location data. Travel and VPNs can trigger it, so I'd start with monitoring before turning on blocking.\n\nYou don't have to use the same preset for everyone. In a service provider's `boot()` method, register a callback:\n\n``` php\nuse App\\SuspiciousLogin\\AdminPreset;\nuse Spatie\\SuspiciousLogin\\Facades\\SuspiciousLogin;\nuse Spatie\\SuspiciousLogin\\Models\\LoginAttempt;\nuse Spatie\\SuspiciousLogin\\Presets\\DefaultPreset;\n\nSuspiciousLogin::determinePresetUsing(function (LoginAttempt $attempt): string {\n    if ($attempt->authenticatable->is_admin) {\n        return AdminPreset::class;\n    }\n\n    return DefaultPreset::class;\n});\n```\n\nHere, `is_admin` is an attribute supplied by your application. Admins use `AdminPreset`, and everyone else uses `DefaultPreset`.\n\nHere's what the default email looks like:\n\nThe email is a regular Laravel mailable, sent to the user's `email` address. You don't need the `Notifiable` trait. To change only the subject, extend the supplied mailable:\n\n``` php\nnamespace App\\Mail;\n\nuse Spatie\\SuspiciousLogin\\Mail\\SuspiciousLoginMail as BaseMail;\n\nclass LoginAlertMail extends BaseMail\n{\n    public function build(): static\n    {\n        return parent::build()->subject('Was this you?');\n    }\n}\n```\n\nThen set `mailable` to `App\\Mail\\LoginAlertMail::class` in the config. You can also supply your own mailable that accepts a `LoginAttempt`, or [publish the email view](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/notifying-users) to change its content.\n\nYou can also listen for events. Here's how to log suspicious logins from a service provider's `boot()` method:\n\n``` php\nuse Illuminate\\Support\\Facades\\Event;\nuse Illuminate\\Support\\Facades\\Log;\nuse Spatie\\SuspiciousLogin\\Events\\SuspiciousLoginDetectedEvent;\n\nEvent::listen(SuspiciousLoginDetectedEvent::class, function (SuspiciousLoginDetectedEvent $event): void {\n    Log::warning('Suspicious login detected', [\n        'login_attempt_id' => $event->loginAttempt->id,\n    ]);\n});\n```\n\nThis event also fires when you're only monitoring logins. You can find all available events [in the documentation](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/using-events).\n\nWe hope you'll like the package. There's more it can do than I've covered here, so be sure to take a look at [the docs](https://spatie.be/docs/laravel-suspicious-login).\n\nYou can find the code [on GitHub](https://github.com/spatie/laravel-suspicious-login) and more examples [in our documentation](https://spatie.be/docs/laravel-suspicious-login).\n\nThis is one of the [open source packages we maintain at Spatie](https://spatie.be/open-source). If you'd like to support that work, take a look at [Flare](https://flareapp.io), [Mailcoach](https://mailcoach.app), [There There](https://there-there.app) and [Oh Dear](https://ohdear.app?utm_source=freek.dev&utm_medium=referral&utm_campaign=blogpost-3230-detecting-suspicious-logins-in-your-laravel-app).", "url": "https://wpnews.pro/news/detecting-suspicious-logins-in-your-laravel-app", "canonical_source": "https://freek.dev/3230-detecting-suspicious-logins-in-your-laravel-app", "published_at": "2026-11-20 10:01:17+00:00", "updated_at": "2026-11-20 10:14:02.567513+00:00", "lang": "en", "topics": ["developer-tools", "ai-agents"], "entities": ["Spatie", "Laravel", "laravel-suspicious-login", "Laravel Boost", "Composer"], "also_reported_by": [], "takeaway": "Spatie's laravel-suspicious-login scores logins by summed signal points (+25 new device, +10 new IP, +30 new country) against default medium/high thresholds of 40 and 70, letting developers map each risk tier to actions like NotifyUserAction or BlockLoginAction.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/detecting-suspicious-logins-in-your-laravel-app", "markdown": "https://wpnews.pro/news/detecting-suspicious-logins-in-your-laravel-app.md", "text": "https://wpnews.pro/news/detecting-suspicious-logins-in-your-laravel-app.txt", "jsonld": "https://wpnews.pro/news/detecting-suspicious-logins-in-your-laravel-app.jsonld"}}