Using events | laravel-suspicious-login | Spatie       

 SPATIE  

laravel-suspicious-login
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-suspicious-login](https://spatie.be/docs/laravel-suspicious-login/v1)  Responding-to-suspicious-logins  Using events

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-suspicious-login/v1) 

  Using events    
- [ Introduction ](https://spatie.be/docs/laravel-suspicious-login/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-suspicious-login/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-suspicious-login/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-suspicious-login/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-suspicious-login/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-suspicious-login/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-suspicious-login/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/getting-started)
- [ How a login is scored ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/how-a-login-is-scored)
- [ Choosing a preset ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/choosing-a-preset)
- [ Customizing presets ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/customizing-presets)
- [ Working with login attempts ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/working-with-login-attempts)
- [ Adding custom properties ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/adding-custom-properties)
- [ Recording logins manually ](https://spatie.be/docs/laravel-suspicious-login/v1/basic-usage/recording-logins-manually)

Available checks
----------------

- [ Overview ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/overview)
- [ New device ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/new-device)
- [ New IP address ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/new-ip)
- [ New country ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/new-country)
- [ Impossible travel ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/impossible-travel)
- [ Allowed and blocked countries ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/country)
- [ 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)
- [ Unusual time ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/unusual-time)
- [ Suspicious user agent ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/suspicious-user-agent)
- [ IP reputation ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/ip-reputation)
- [ Multiple countries ](https://spatie.be/docs/laravel-suspicious-login/v1/available-checks/multiple-countries)

Responding to suspicious logins
-------------------------------

- [ Notifying users ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/notifying-users)
- [ Blocking logins ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/blocking-logins)
- [ Writing your own actions ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/writing-your-own-actions)
- [ Using events ](https://spatie.be/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/using-events)

Advanced usage
--------------

- [ Creating custom checks ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/creating-custom-checks)
- [ Geolocation drivers ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/geolocation-drivers)
- [ IP reputation drivers ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/ip-reputation-drivers)
- [ Device fingerprints ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/device-fingerprints)
- [ Using your own model ](https://spatie.be/docs/laravel-suspicious-login/v1/advanced-usage/using-your-own-model)

 Using events
============

###  On this page 

1. [ LoginAttemptRecordedEvent ](#content-loginattemptrecordedevent)
2. [ SuspiciousLoginDetectedEvent ](#content-suspiciouslogindetectedevent)
3. [ SuspiciousLoginBlockedEvent ](#content-suspiciousloginblockedevent)
4. [ Notifying admins of blocked logins ](#content-notifying-admins-of-blocked-logins)
5. [ Listeners run during the login ](#content-listeners-run-during-the-login)

The package fires events at the key moments of a login. Listen for them to build a dashboard, forward logins to your logging or security tools, or alert an admin, without touching your preset.

Every event has one public property, `loginAttempt`, that holds the `Spatie\SuspiciousLogin\Models\LoginAttempt`.

LoginAttemptRecordedEvent
-----------------------------------------------------------------------------------------------------------------------------------

`Spatie\SuspiciousLogin\Events\LoginAttemptRecordedEvent` is fired for every login attempt, successful or failed, right after it is saved.

For a successful login, it's fired after the login has been scored, so `risk_score`, `risk_level` and `reasons` are filled in, but before the preset's actions run. For a failed login, those attributes are `null`.

This is the event to use with the `MonitorOnly` preset, because it also tells you about low risk logins.

SuspiciousLoginDetectedEvent
--------------------------------------------------------------------------------------------------------------------------------------------

`Spatie\SuspiciousLogin\Events\SuspiciousLoginDetectedEvent` is fired when a successful login is medium or high risk. It's fired right before the preset's actions run, and it's fired with every preset, `MonitorOnly` included.

SuspiciousLoginBlockedEvent
-----------------------------------------------------------------------------------------------------------------------------------------

`Spatie\SuspiciousLogin\Events\SuspiciousLoginBlockedEvent` is fired by [`BlockLoginAction`](/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/blocking-logins), after it has marked the attempt as blocked and before it logs the user out.

Notifying admins of blocked logins
--------------------------------------------------------------------------------------------------------------------------------------------------------------

If your app already has a `LoginBlockedNotification` configured for Slack, create `app/Listeners/NotifyAdminsOfBlockedLogin.php`:

```
namespace App\Listeners;

use App\Notifications\LoginBlockedNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Notification;
use Spatie\SuspiciousLogin\Events\SuspiciousLoginBlockedEvent;

class NotifyAdminsOfBlockedLogin implements ShouldQueue
{
    public function handle(SuspiciousLoginBlockedEvent $event): void
    {
        Notification::route('slack', config('services.slack.security_channel'))
            ->notify(new LoginBlockedNotification($event->loginAttempt));
    }
}
```

`LoginBlockedNotification` is an application notification, not a class supplied by this package. It should accept the attempt in its constructor. Install and configure Laravel's Slack notification channel and set `services.slack.security_channel` to the routing value that channel expects. Use an asynchronous queue connection and run a queue worker so delivery happens outside the login request.

Laravel discovers listeners in `app/Listeners` automatically. If you've turned event discovery off, register the listener in a service provider:

```
use App\Listeners\NotifyAdminsOfBlockedLogin;
use Illuminate\Support\Facades\Event;
use Spatie\SuspiciousLogin\Events\SuspiciousLoginBlockedEvent;

Event::listen(SuspiciousLoginBlockedEvent::class, NotifyAdminsOfBlockedLogin::class);
```

Listeners run during the login
--------------------------------------------------------------------------------------------------------------------------------------------------

The events are fired during the login request. A slow listener makes logging in slow, so let listeners that call external services implement `ShouldQueue`.

A listener that isn't queued can still affect the login. Throwing an exception stops the request, but does not log the user out or mark the attempt as blocked. To reject a session login, use [`BlockLoginAction`](/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/blocking-logins). For other authentication flows, write a [custom action](/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/writing-your-own-actions) that rejects the login and cleans up any authentication state it created.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
