Recording logins manually | 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)  Basic-usage  Recording logins manually

 Version   v1      

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

  Recording logins manually    
- [ 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)

 Recording logins manually
=========================

###  On this page 

1. [ Scoring without saving ](#content-scoring-without-saving)

The package listens for Laravel's `Illuminate\Auth\Events\Login` and `Illuminate\Auth\Events\Failed` events. Laravel's session guard fires `Login` for successful calls to `Auth::attempt()` or `Auth::login()`, and `Failed` when `Auth::attempt()` rejects credentials. Starter kits like Breeze and Fortify use these events, so usually you don't need to do anything.

If your app authenticates users in a way that doesn't fire those events, for example by issuing API tokens in your own controller, you can record the logins yourself:

This example uses the default `Balanced` preset, which notifies but never blocks. Use it after validating the request's email and password fields, and before issuing a token:

```
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Spatie\SuspiciousLogin\Facades\SuspiciousLogin;

$user = User::firstWhere('email', $request->email);

if (! $user || ! Hash::check($request->password, $user->password)) {
    SuspiciousLogin::recordFailedLogin($user, identifier: $request->email, guard: 'api');

    abort(401);
}

$attempt = SuspiciousLogin::recordSuccessfulLogin($user, guard: 'api');

if ($attempt->isSuspicious()) {
    // ...
}
```

`recordSuccessfulLogin` scores the login and runs the preset's actions, exactly like a login through Laravel's events. Both methods take the current request by default. Pass a `request:` argument to record a login for a different request, and a `properties:` argument to store [custom properties](/docs/laravel-suspicious-login/v1/basic-usage/adding-custom-properties) with it.

For token authentication, record and assess the attempt before issuing the token. Use a preset whose actions support your authentication flow. `BlockLoginAction` calls `logout()` on a session guard; it cannot revoke an API token or log out a stateless guard. Write a [custom action](/docs/laravel-suspicious-login/v1/responding-to-suspicious-logins/writing-your-own-actions) to mark the attempt as blocked and reject token issuance when needed. The `guard:` argument records the guard name; it does not authenticate the user.

Both recording methods work even when `suspicious-login.enabled` is `false`. That setting only disables the automatic auth-event listeners.

Don't record a login manually when Laravel already fires its events for it, or it will be stored twice.

Scoring without saving
--------------------------------------------------------------------------------------------------------------------------

To find out how risky a login would be without recording it, create an unsaved attempt and pass it to `assess`:

```
use Spatie\SuspiciousLogin\Facades\SuspiciousLogin;
use Spatie\SuspiciousLogin\Fingerprinters\DeviceFingerprinter;
use Spatie\SuspiciousLogin\Models\LoginAttempt;

$location = SuspiciousLogin::locate($request->ip());

$attempt = new LoginAttempt([
    'successful' => true,
    'ip_address' => $request->ip(),
    'user_agent' => $request->userAgent(),
    'device_fingerprint' => app(DeviceFingerprinter::class)->fingerprint($request),
    'country_code' => $location?->countryCode,
    'latitude' => $location?->latitude,
    'longitude' => $location?->longitude,
    'created_at' => now(),
]);

$attempt->authenticatable()->associate($user);

SuspiciousLogin::assess($attempt);

$attempt->risk_score; // the score for the supplied attributes and the user's history
$attempt->reasons; // the checks that flagged it
```

`assess` runs the checks and fills in the score, level and reasons, but doesn't save the attempt or run any actions. It only looks at the attributes you set, so fill in the fingerprint and location yourself, as above.

 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)
