Installation &amp; setup | 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)  Installation &amp; setup

 Version   v1      

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

  Installation &amp; setup    
- [ 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)

 Installation &amp; setup
========================

###  On this page 

1. [ Register the package ](#content-register-the-package)
2. [ Create the migration ](#content-create-the-migration)
3. [ Add browser middleware and authentication ](#content-add-browser-middleware-and-authentication)
4. [ The config ](#content-the-config)
5. [ Geolocation ](#content-geolocation)
6. [ Pruning old login attempts ](#content-pruning-old-login-attempts)
7. [ Agent guidance ](#content-agent-guidance)

You can install the package with Go:

```
go get github.com/spatie/laravel-suspicious-login-go@v0.1.0
```

Register the package
--------------------------------------------------------------------------------------------------------------------

Add the package to `laravel.Options.Packages` in `bootstrap/app.go`. Explicit configuration callbacks run after typed environment defaults:

```
import (
    "context"

    "github.com/spatie/laravel-suspicious-login-go"
    laravel "go.laravel.com"
    "go.laravel.com/db"
    "go.laravel.com/model"

    "app/models"
)

func Options() laravel.Options {
    return laravel.Options{
        Packages: []laravel.Package{
            suspiciouslogin.New(func(cfg *suspiciouslogin.Config) {
                cfg.Email = func(ctx context.Context, user suspiciouslogin.User) (string, error) {
                    if user.Type != "users" || user.ID == "" {
                        return "", nil
                    }
                    account, err := model.Find[models.User](ctx, user.ID)
                    if db.IsNotFound(err) {
                        return "", nil
                    }
                    return account.Email, err
                }
            }),
        },
    }
}
```

`models.User` is your application's user type. `Email` resolves the current recipient, skipping deleted users and empty addresses. Without this callback, `NotifyUserAction` has no recipient and sends nothing.

Create the migration
--------------------------------------------------------------------------------------------------------------------

Add an application-owned migration to your `migrations.All` slice, already wired into `Options.Migrations`:

```
db.Migration{Name: "create_login_attempts", Run: suspiciouslogin.CreateTable},
```

Run the application's migrations before recording logins:

```
go run . migrate
```

The package does not automatically register the table. Applications [replacing the model](/docs/laravel-suspicious-login-go/v1/advanced-usage/using-your-own-model) own the replacement schema instead. There is no PHP install command or config-publishing step.

Add browser middleware and authentication
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Append the device middleware to the web group, inside Laravel's `EncryptCookies`. Keep the normal web middleware and add session authentication once:

```
Middleware: func(m *laravel.Middleware, _ *config.Config) {
    m.Web.Append(auth.Session[models.User]{}, suspiciouslogin.DeviceCookie{})
},
```

Import `go.laravel.com/auth` and `go.laravel.com/config` for this callback. Do not add the device cookie to encryption exceptions or install `DeviceCookie` globally. Configure trusted proxies correctly when HTTPS terminates at a proxy. Stateless apps can [choose user-agent fingerprinting](/docs/laravel-suspicious-login-go/v1/advanced-usage/device-fingerprints) instead.

Laravel Go v0.1.0 emits no authentication events. Replace session calls to `auth.Attempt` and `auth.Login` with the package's `Authenticator` helpers. See [getting started](/docs/laravel-suspicious-login-go/v1/basic-usage/getting-started) for the handler. Restored remember-me sessions are not new logins and are not recorded.

The config
--------------------------------------------------------------------------------------

Code callbacks can replace every integration dependency:

FieldDefault`Enabled``true`, controls `Authenticator` recording only`Preset``DefaultPreset()``DeterminePreset`nil, uses `Preset``Properties`nil`Store``ModelStore{}``Fingerprinter``CookieFingerprinter{}``Geolocation``IPInfoDriver{Token: ...}``Reputation``NullReputation{}``LocationTTL`, `ReputationTTL`One week and one day`PruneAfterDays`365`Email`nil, skips warning delivery`Mailable`Factory for `SuspiciousLoginMail``Mailer`, `QueueConnection`, `Queue`Empty, uses Laravel defaultsThese environment settings are read at registration:

VariableDefault`SUSPICIOUS_LOGIN_ENABLED``true``SUSPICIOUS_LOGIN_MEDIUM_THRESHOLD``40``SUSPICIOUS_LOGIN_HIGH_THRESHOLD``70``SUSPICIOUS_LOGIN_LOCATION_TTL``604800` seconds`SUSPICIOUS_LOGIN_REPUTATION_TTL``86400` seconds`SUSPICIOUS_LOGIN_PRUNE_AFTER_DAYS``365``SUSPICIOUS_LOGIN_MAILER`Empty`SUSPICIOUS_LOGIN_QUEUE_CONNECTION`Empty`SUSPICIOUS_LOGIN_QUEUE`Empty`IPINFO_TOKEN`EmptyThe two threshold environment settings configure the initial default preset. A replacement preset supplies its own values. Invalid thresholds or negative retention and cache durations stop startup. A cache TTL of zero bypasses that per-IP cache. Treat the final configuration as immutable after registration.

Geolocation
-----------------------------------------------------------------------------------------

The default IPInfo driver sends public IP addresses to ipinfo.io without requiring a token. Results are cached per IP for a week. Requests are subject to ipinfo's limits. Failed lookups leave the country and coordinates unknown, and checks that need that data pass.

To avoid sending IP addresses to an external service:

```
cfg.Geolocation = suspiciouslogin.NullGeolocation{}
```

Previously cached locations remain until expiry. Set `LocationTTL` to zero or forget the per-IP cache key when you need the change to take effect immediately. See [geolocation drivers](/docs/laravel-suspicious-login-go/v1/advanced-usage/geolocation-drivers).

Pruning old login attempts
--------------------------------------------------------------------------------------------------------------------------------------

Login attempts pile up. Schedule a daily callback in your application's schedule:

```
s.Call("prune-login-attempts", func(ctx context.Context) error {
    _, err := suspiciouslogin.From(ctx).Prune(ctx)
    return err
}).Daily()
```

`s` is Laravel's `*schedule.Schedule`. Configure the host scheduler as usual. `Prune` deletes attempts at or before the retention cutoff using the configured store. Pruning very aggressively makes known devices and countries unfamiliar again.

Agent guidance
--------------------------------------------------------------------------------------------------

The repository includes the Spatie Go package skill and version-matched Laravel Go skills under `.agents/skills/`. Read `AGENTS.md` when contributing. PHP's Laravel Boost installation and vendor publishing commands do not apply to Go.

 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)
