Laravel Suspicious Login: Risk-Based Login Detection | Mohamed Said       [Skip to content](#main)  [ ![](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png) Mohamed SaidLaravel Backend Engineer ](https://msaied.com) - [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [About](https://msaied.com#about)

           [  Contact](https://msaied.com#contact) Menu 

Menu
----

Close 

 - [HomeStart here](https://msaied.com)
- [ProjectsCase studies](https://msaied.com/projects)
- [ArticlesEngineering notes](https://msaied.com/articles)
- [CertificatesCredentials](https://msaied.com/certificates)
- [AboutHow I work](https://msaied.com#about)
- [ContactGet in touch](https://msaied.com#contact)

  [Start a conversation](https://msaied.com#contact) [WhatsApp](https://wa.me/201094619204) [Email](mailto:hello@msaied.com) 

 1. [Home](https://msaied.com)
2. /
3. [Articles](https://msaied.com/articles)
4. /
5. [Laravel](https://msaied.com/articles?category=laravel)
6. /
7. Laravel Suspicious Login: Detect and Block Risky Logins with Risk Scoring

   [Laravel](https://msaied.com/articles?category=laravel) [Composer Pacakge](https://msaied.com/articles?category=composer-pacakge) 

 Laravel Suspicious Login: Detect and Block Risky Logins with Risk Scoring
==========================================================================

 Spatie's laravel-suspicious-login package scores every successful login by device, IP, location, and history, then emails users or blocks the session based on configurable risk thresholds.

 ![](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp) [Mohamed Said](https://msaied.com#person) Published 23 Nov 2026 · Updated 26 Nov 2026 · 4 min read

ShareCopy linkCopied

 ![Laravel Suspicious Login: Detect and Block Risky Logins with Risk Scoring](https://cdn.msaied.com/836/db1492ae4c7344b53fc65bb37075cf40.png) 

  On this page +1. [What Is Laravel Suspicious Login?](#what-is-laravel-suspicious-login)
2. [How Risk Scoring Works](#how-risk-scoring-works)
3. [Presets: Bundled and Custom](#presets-bundled-and-custom)
4. [Per-User Presets](#per-user-presets)
5. [Blocking Logins and Custom Actions](#blocking-logins-and-custom-actions)
6. [Device Identification and Installation](#device-identification-and-installation)
7. [Key Takeaways](#key-takeaways)

 What Is Laravel Suspicious Login?
---------------------------------

Spatie has released `laravel-suspicious-login`, a package by Freek Van der Herten that assigns a numeric risk score to every successful login. The score is built from four signals: the device used, the IP address, the geographic location, and the user's past login history. Once scored, the package takes an action you configure — send an email, block the login, or run custom code.

### How Risk Scoring Works

Each check contributes points to a total score. The package ships with two default thresholds:

- **Medium risk:** score ≥ 40 → notify the user by email
- **High risk:** score ≥ 70 → configurable; can block the login

A concrete example: a user who always logs in from Toronto authenticates from a new phone in Lisbon.

```php
New device                    +25
New IP address                +10
New country (PT)              +30
                              ---
Risk score                     65  → medium risk, email sent

```

Thresholds live in `config/suspicious-login.php` and are easy to adjust:

```php
'risk_thresholds' => [
    'medium' => 40,
    'high'   => 70,
],

```

Presets: Bundled and Custom
---------------------------

A **preset** is a class that declares which checks to run, the score for each, and the actions to fire at each risk level. Three presets ship out of the box:

- `DefaultPreset` — standard checks, emails the user on medium risk
- `MonitorOnlyPreset` — records scores and fires events, no emails or blocks
- `NonePreset` — records the attempt and fires `LoginAttemptRecordedEvent`, skips scoring entirely

You can write your own by extending `Preset`. Here is a `PayrollPreset` for an app where all staff work in Canada or the United States:

```php
class PayrollPreset extends Preset
{
    public function checks(): array
    {
        return [
            NewDeviceCheck::new()->score(20),
            CountryCheck::new()->onlyAllow(['CA', 'US'])->score(50),
            ImpossibleTravelCheck::new()->maxSpeedInKilometersPerHour(900)->score(70),
        ];
    }

    public function onMediumRisk(): Action|array
    {
        return NotifyUserAction::new();
    }

    public function onHighRisk(): Action|array
    {
        return [
            NotifyUserAction::new(),
            BlockLoginAction::new()->message('We blocked this sign-in. Contact your admin.'),
        ];
    }
}

```

Register it in the config:

```php
'preset' => App\SuspiciousLogin\PayrollPreset::class,

```

Per-User Presets
----------------

You can return a different preset for each login attempt by registering a callback in a service provider's `boot()` method:

```php
SuspiciousLogin::determinePresetUsing(function (LoginAttempt $attempt): string {
    if ($attempt->guard === 'staff') {
        return PayrollPreset::class;
    }
    if ($attempt->authenticatable->email === 'demo@example.com') {
        return NonePreset::class;
    }
    return DefaultPreset::class;
});

```

The callback must always return a class name string — it cannot return `null`.

Blocking Logins and Custom Actions
----------------------------------

`BlockLoginAction` marks the attempt as blocked, fires `SuspiciousLoginBlockedEvent`, logs the user out, and throws a `ValidationException` that surfaces as a standard form error. Place it last in any action array because it halts execution.

Custom actions extend `Action` and implement `execute(LoginAttempt $attempt)`. Custom checks extend `Check` and return `$this->pass()` or `$this->flag($reason)` from `evaluate()`.

Device Identification and Installation
--------------------------------------

The package uses an encrypted cookie to identify devices — no JavaScript required. Two browsers with the same user agent still get distinct identities. Cookie-less or stateless apps can switch to the user-agent fingerprinter or provide their own identifier.

Requirements: **PHP 8.4**, Laravel 11, 12, or 13.

```bash
composer require spatie/laravel-suspicious-login
php artisan suspicious-login:install

```

The install command publishes the config and a migration for the `login_attempts` table. The package hooks into Laravel's built-in `Login` and `Failed` auth events, so it works with Breeze, Jetstream, Fortify, and custom controllers without any code changes.

Key Takeaways
-------------

- Risk scores are built from device, IP, country, and travel-speed checks
- Three built-in presets cover most needs; custom presets are straightforward to write
- Per-user preset selection is supported via a single callback
- `BlockLoginAction` throws a `ValidationException`, keeping the UX consistent with normal form errors
- Start with `MonitorOnlyPreset` on existing apps to review real scores before enabling notifications or blocks
- Geolocation uses ipinfo by default; MaxMind (`geoip2/geoip2`) is also supported
- Requires PHP 8.4 and is compatible with Laravel 11–13

Source: [Laravel Suspicious Login — Laravel News](https://laravel-news.com/laravel-suspicious-login)

- [security](https://msaied.com/articles?search=security)
- [authentication](https://msaied.com/articles?search=authentication)
- [spatie](https://msaied.com/articles?search=spatie)
- [laravel-package](https://msaied.com/articles?search=laravel-package)
- [login-protection](https://msaied.com/articles?search=login-protection)

 Frequently asked questions 
---------------------------

  Does laravel-suspicious-login work with Laravel Breeze, Jetstream, and Fortify?Yes. The package listens to Laravel's built-in Login and Failed auth events, so it integrates with Breeze, Jetstream, Fortify, and custom login controllers without any code changes.

   What happens when a login is blocked by BlockLoginAction?BlockLoginAction marks the attempt as blocked, fires SuspiciousLoginBlockedEvent, logs the user out of the guard they used, and throws a ValidationException. The exception surfaces as a normal validation error on the login form, on the email field by default.

   How should I roll out this package on an existing production app?Start with MonitorOnlyPreset. It records scores and fires events without emailing or blocking anyone. Review the scores for real logins first, then switch to DefaultPreset or a custom preset once you are confident in your thresholds.

   ![Mohamed Said](https://cdn.msaied.com/01M22N44A70A5MC2S599JP0MPH.webp)About the author
----------------

[Mohamed Said](https://msaied.com#person)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

[About](https://msaied.com#about) [GitHub ↗](https://github.com/EG-Mohamed) [LinkedIn ↗](https://www.linkedin.com/in/msaiedm/) [WhatsApp ↗](https://wa.me/201094619204) [Email Address ↗](mailto:hello@msaied.com) [My CV ↗](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)  

   [Previous articleDecide with Jev Episode 5: Make Failed AI Requests Safe in Laravel](https://msaied.com/articles/decide-with-jev-episode-5-make-failed-ai-requests-safe-in-laravel) [Next articleLaravel Black Friday and Cyber Monday Deals 2026](https://msaied.com/articles/laravel-black-friday-and-cyber-monday-deals-2026)  

   On this page
-------------

1. [What Is Laravel Suspicious Login?](#what-is-laravel-suspicious-login)
2. [How Risk Scoring Works](#how-risk-scoring-works)
3. [Presets: Bundled and Custom](#presets-bundled-and-custom)
4. [Per-User Presets](#per-user-presets)
5. [Blocking Logins and Custom Actions](#blocking-logins-and-custom-actions)
6. [Device Identification and Installation](#device-identification-and-installation)
7. [Key Takeaways](#key-takeaways)

 ###  Have a technical challenge?

 Tell me what you’re building. I reply within two working days.

[Start a conversation](https://msaied.com#contact) 

   Related articles
-----------------

 [ ![](https://cdn.msaied.com/834/bd4177ac22f4bab6c85e743e4f0051e6.png) Laravel · 3 min read### Laravel Black Friday and Cyber Monday Deals 2026

23 Nov 2026 ](https://msaied.com/articles/laravel-black-friday-and-cyber-monday-deals-2026) [ ![](https://cdn.msaied.com/835/85710585fb2cbe5fd3fbf12fe76c647b.png) Laravel · 4 min read### Decide with Jev Episode 5: Make Failed AI Requests Safe in Laravel

22 Nov 2026 ](https://msaied.com/articles/decide-with-jev-episode-5-make-failed-ai-requests-safe-in-laravel) [ ![](https://cdn.msaied.com/832/68ae93269d6d7cdcbb51731f2a186f88.png) Filament · 3 min read### Filament v3.3.58 Released: Bug Fixes for Schemas, Actions, and Panel Context

21 Nov 2026 ](https://msaied.com/articles/filament-v3358-released-bug-fixes-for-schemas-actions-and-panel-context) 

  Have a technical challenge?
----------------------------

Tell me what you’re building. I reply within two working days.

 [Discuss your project ↗](https://msaied.com#contact) 

  © 2026 Mohamed Said · Built with Laravel, meant to last.Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

 - [Home](https://msaied.com)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [GitHub](https://github.com/EG-Mohamed)
- [LinkedIn](https://www.linkedin.com/in/msaiedm/)
- [WhatsApp](https://wa.me/201094619204)
- [Email Address](mailto:hello@msaied.com)
- [My CV](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
- [Sitemap](https://msaied.com/sitemap.xml)
