Getting started | 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  Getting started

 Version   v1      

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

  Getting started    
- [ 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)

 Getting started
===============

###  On this page 

1. [ Create a preset ](#content-create-a-preset)
2. [ Wire the login handler ](#content-wire-the-login-handler)
3. [ What happens at login ](#content-what-happens-at-login)
4. [ Customize further ](#content-customize-further)

After [installing the package](/docs/laravel-suspicious-login-go/v1/installation-setup), this example adds suspicious-login detection to a Laravel Go application with session authentication. We'll notify users when they log in from a new country, and block the login when the device is new too.

Create a preset
-----------------------------------------------------------------------------------------------------

A preset chooses the checks to run, their scores, and the actions for medium and high risk:

```
func loginPreset() suspiciouslogin.Preset {
    p := suspiciouslogin.DefaultPreset()
    p.Checks = []suspiciouslogin.Check{
        suspiciouslogin.NewDeviceCheck{Score: 30},
        suspiciouslogin.NewCountryCheck{Score: 40},
    }
    p.OnHigh = []suspiciouslogin.Action{
        suspiciouslogin.NotifyUserAction{},
        suspiciouslogin.BlockLoginAction{},
    }
    return p
}
```

Each check adds its score when it flags the login. The thresholds default to 40 for medium risk and 70 for high risk. `NotifyUserAction` queues email, and `BlockLoginAction` persists the block, logs the user out and returns a validation error. Put blocking last because it ends the request. High risk runs only `OnHigh`, not the medium-risk actions as well.

Select the preset in your registration callback, alongside the `Email` resolver from installation:

```
cfg.Preset = loginPreset()
```

Wire the login handler
--------------------------------------------------------------------------------------------------------------------------

Laravel Go v0.1.0 has no login events. Call `Authenticator.Attempt` instead of `auth.Attempt` in your existing handler after validating the input:

```
import (
    "strconv"

    "github.com/spatie/laravel-suspicious-login-go"
    "go.laravel.com/auth"
    "go.laravel.com/web"

    "app/models"
)

func authenticate(r *web.Request, email, password string, remember bool) *web.Response {
    login := suspiciouslogin.Authenticator[models.User]{
        Users: auth.ModelUsers[models.User]{},
        Identify: func(user *models.User) suspiciouslogin.User {
            return suspiciouslogin.User{Type: "users", ID: strconv.FormatInt(user.ID, 10)}
        },
        Guard: "web",
    }
    if err := login.Attempt(r, email, password, remember); err != nil {
        return web.Fail(err)
    }
    return auth.RedirectIntended("/dashboard")
}
```

Use the same user provider in `Users` as the host's auth configuration. `Identify` supplies a stable model type and string ID for history and email lookup. For an already validated user, call `login.Login(r, user, remember)`. The guard string is stored as metadata, not used to select another Go guard. Set `ErrorKey` when invalid credentials should attach to a field other than `email`. Keep login rate limiting and normal input validation in your application.

What happens at login
-----------------------------------------------------------------------------------------------------------------------

Suppose Anna has already logged in successfully from her usual browser in Belgium. With this preset:

LoginScoreRiskResultKnown device, known country0lowRecord the attempt, no actionNew device, known country30lowRecord the attempt, no actionKnown device, new country40mediumRecord the attempt and notify AnnaNew device, new country70highRecord, notify Anna and block the loginThe first login has no history to compare against, so these two checks don't flag it. Failed logins are recorded but aren't scored, and failed or blocked attempts don't make a device or country familiar. VPNs and legitimate travel can trigger country checks. Choose scores and actions that suit your application.

Customize further
-----------------------------------------------------------------------------------------------------------

- [Inspect login attempts](/docs/laravel-suspicious-login-go/v1/basic-usage/working-with-login-attempts) to see scores, reasons and blocks.
- [Choose a preset per login](/docs/laravel-suspicious-login-go/v1/basic-usage/choosing-a-preset) to apply stricter checks to admins or skip assessment.
- [Add custom properties](/docs/laravel-suspicious-login-go/v1/basic-usage/adding-custom-properties) to store application context.
- [Record logins manually](/docs/laravel-suspicious-login-go/v1/basic-usage/recording-logins-manually) for token flows. Don't combine manual recording with the helper for the same login.
- [Write your own checks](/docs/laravel-suspicious-login-go/v1/advanced-usage/creating-custom-checks), [actions](/docs/laravel-suspicious-login-go/v1/responding-to-suspicious-logins/writing-your-own-actions) or [event listeners](/docs/laravel-suspicious-login-go/v1/responding-to-suspicious-logins/using-events).

 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)
