cd /news/developer-tools/detecting-suspicious-logins-in-your-… · home › topics › developer-tools › article
[ARTICLE · art-173163] src=freek.dev ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

★ Detecting suspicious logins in your Laravel app

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.

key 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.

by read4 min views2 publishedNov 20, 2026

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.

We've released 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.

You 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.

Let's take a look at how the package detects signals that might indicate a suspicious login.

An 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.

Each 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:

New device       +25
New IP address   +10
New country      +30
                 ---
Total             65

Assuming the other checks pass, that's medium risk. The default thresholds in config/suspicious-login.php are:

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

The default preset also checks for impossible travel, repeated failed attempts, credential stuffing and suspicious user agents.

The package also logs all 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.

A 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.

Let's look at this example preset implementation:

namespace App\SuspiciousLogin;

use Spatie\SuspiciousLogin\Actions\Action;
use Spatie\SuspiciousLogin\Actions\BlockLoginAction;
use Spatie\SuspiciousLogin\Actions\NotifyUserAction;
use Spatie\SuspiciousLogin\Checks\NewCountryCheck;
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(30),
            NewCountryCheck::new()->score(40),
        ];
    }

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

    public function onHighRisk(): Action|array
    {
        return [
            NotifyUserAction::new(),
            BlockLoginAction::new(),
        ];
    }
}

Don't forget to specify your preset class in the config file:

use App\SuspiciousLogin\AdminPreset;

'preset' => AdminPreset::class,

A 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.

A 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.

The 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.

You don't have to use the same preset for everyone. In a service provider's boot() method, register a callback:

use App\SuspiciousLogin\AdminPreset;
use Spatie\SuspiciousLogin\Facades\SuspiciousLogin;
use Spatie\SuspiciousLogin\Models\LoginAttempt;
use Spatie\SuspiciousLogin\Presets\DefaultPreset;

SuspiciousLogin::determinePresetUsing(function (LoginAttempt $attempt): string {
    if ($attempt->authenticatable->is_admin) {
        return AdminPreset::class;
    }

    return DefaultPreset::class;
});

Here, is_admin is an attribute supplied by your application. Admins use AdminPreset, and everyone else uses DefaultPreset.

Here's what the default email looks like:

The 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:

namespace App\Mail;

use Spatie\SuspiciousLogin\Mail\SuspiciousLoginMail as BaseMail;

class LoginAlertMail extends BaseMail
{
    public function build(): static
    {
        return parent::build()->subject('Was this you?');
    }
}

Then 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 to change its content.

You can also listen for events. Here's how to log suspicious logins from a service provider's boot() method:

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Spatie\SuspiciousLogin\Events\SuspiciousLoginDetectedEvent;

Event::listen(SuspiciousLoginDetectedEvent::class, function (SuspiciousLoginDetectedEvent $event): void {
    Log::warning('Suspicious login detected', [
        'login_attempt_id' => $event->loginAttempt->id,
    ]);
});

This event also fires when you're only monitoring logins. You can find all available events in the documentation.

We 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.

You can find the code on GitHub and more examples in our documentation.

This is one of the open source packages we maintain at Spatie. If you'd like to support that work, take a look at Flare, Mailcoach, There There and Oh Dear.

── more in #developer-tools 4 stories · sorted by recency
── more on @spatie 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/detecting-suspicious…] indexed:0 read:4min 2026-11-20 · —