Working with login attempts | laravel-suspicious-login-go | Spatie       

 SPATIE  

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-suspicious-login-go](https://spatie.be/docs/laravel-suspicious-login-go/v1)  Basic-usage  Working with login attempts

 Version   v1      

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

  Working with login attempts    
- [ Introduction ](https://spatie.be/docs/laravel-suspicious-login-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-suspicious-login-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-suspicious-login-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-suspicious-login-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-suspicious-login-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-suspicious-login-go/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-suspicious-login-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-suspicious-login-go/v1/releasing)
- [ About us ](https://spatie.be/docs/laravel-suspicious-login-go/v1/about-us)

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

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

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

- [ Overview ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/overview)
- [ New device ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/new-device)
- [ New IP address ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/new-ip)
- [ New country ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/new-country)
- [ Impossible travel ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/impossible-travel)
- [ Allowed and blocked countries ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/country)
- [ Failed attempts ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/failed-attempts)
- [ Credential stuffing ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/credential-stuffing)
- [ Unusual time ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/unusual-time)
- [ Suspicious user agent ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/suspicious-user-agent)
- [ IP reputation ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/ip-reputation)
- [ Multiple countries ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/multiple-countries)

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

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

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

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

 Working with login attempts
===========================

###  On this page 

1. [ What is stored ](#content-what-is-stored)
2. [ Inspecting scores and reasons ](#content-inspecting-scores-and-reasons)
3. [ Showing users their recent logins ](#content-showing-users-their-recent-logins)
4. [ Useful methods and queries ](#content-useful-methods-and-queries)

Every login and failed login is stored as a `LoginAttempt`. Use these records to show recent activity, build an admin overview, or see how your preset scores real logins.

What is stored
--------------------------------------------------------------------------------------------------

AttributeContains`UserType`, `UserID`Stable account reference, empty for an unknown failed identifier`Guard`Login flow name, recorded as metadata`Identifier`Failed email address or username, never a password`Successful`Whether credentials succeeded, even if later blocked`Blocked`Whether a blocking action rejected the login`IPAddress`, `UserAgent`Taken from the request`DeviceFingerprint`Hash identifying the browser or device`CountryCode`, `City`, `Latitude`, `Longitude`Location, if available`RiskScore`, `RiskLevel`Nullable pointers, set only after assessment`Reasons`Nullable `*db.JSON[[]Reason]`, each with check, score and explanation`Properties`Nullable `*db.JSON[map[string]any]` with custom properties`CreatedAt`Attempt timestamp`attempt.User()` returns the stable `User` reference, not a lazy-loaded account. Resolve your own model when needed. JSON column wrappers serialize directly as their contained values.

Inspecting scores and reasons
-----------------------------------------------------------------------------------------------------------------------------------------------

For the default model, query with Laravel's native model API:

```
attempts, err := model.Query[suspiciouslogin.LoginAttempt]().
    WhereEq("user_type", "users").WhereEq("user_id", userID).
    Latest("created_at").OrderByDesc("id").Limit(10).Get(ctx)
```

Check `err` before displaying results. `IsSuspicious()` is true for medium and high risk. `Reasons` is nil for failed and unassessed logins, otherwise read `Reasons.V`. `Blocked` distinguishes a rejected login from an authenticated one.

Showing users their recent logins
-----------------------------------------------------------------------------------------------------------------------------------------------------------

Add `WhereEq("successful", true)` to the query above. Add `WhereEq("blocked", false)` when you only want trusted logins. Always scope both user type and ID before displaying history to a user.

Useful methods and queries
--------------------------------------------------------------------------------------------------------------------------------------

```
attempt.IsSuspicious()
attempt.Trusted()
attempt.Location()
attempt.Property("tenant_id", nil)

history, err := suspiciouslogin.From(ctx).Store.History(ctx, &attempt)
```

History contains the other attempts of the same user up to this timestamp, newest first. It includes failures and blocks, so custom checks must filter with `Trusted()` when learning familiar behavior. History is scoped to the user, not the guard.

Use `WhereEq("successful", false)` for failures and `WhereGe("created_at", cutoff)` for a time window. With a [replacement store](/docs/laravel-suspicious-login-go/v1/advanced-usage/using-your-own-model), query your own model instead of the package table.

 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)
