★ 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. 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 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. 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: js 'risk thresholds' = 'medium' = 40, 'high' = 70, , The 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 . The 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 . 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: php 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: php 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: php 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: php 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 https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/notifying-users to change its content. You can also listen for events. Here's how to log suspicious logins from a service provider's boot method: php 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 https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/using-events . 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 https://spatie.be/docs/laravel-suspicious-login . You 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 . This 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 .