PHPackages                             alajusticia/laravel-sanctum-tracker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. alajusticia/laravel-sanctum-tracker

AbandonedArchivedLibrary[Authentication &amp; Authorization](/categories/authentication)

alajusticia/laravel-sanctum-tracker
===================================

Track Sanctum tokens in Laravel.

v1.2.2(2y ago)45.2k↓78%3MITPHPPHP ^7.4|^8.0

Since Apr 8Pushed 2y ago1 watchersCompare

[ Source](https://github.com/alajusticia/laravel-sanctum-tracker)[ Packagist](https://packagist.org/packages/alajusticia/laravel-sanctum-tracker)[ RSS](/packages/alajusticia-laravel-sanctum-tracker/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (6)Dependencies (12)Versions (7)Used By (0)

Laravel Sanctum Tracker
=======================

[](#laravel-sanctum-tracker)

> ⚠️ This package is now archived. Use [Laravel Logins](https://github.com/alajusticia/laravel-logins) instead.

#### Track Sanctum tokens in Laravel.

[](#track-sanctum-tokens-in-laravel)

This package allows you to track Sanctum tokens, attaching informations by parsing the User-Agent and saving the IP address.

Using a supported provider or creating your own custom providers, you can collect even more informations with an IP address lookup to get, for example, the geolocation.

It also provides a trait introducing convenient methods: `logout`, `logoutOthers` and `logoutAll` for your user model.

- [Compatibility](#compatibility)
- [Installation](#installation)
    - [Override default model](#override-default-model)
    - [Choose and install a user-agent parser](#choose-and-install-a-user-agent-parser)
    - [Add the trait to your user model (optional)](#add-the-trait-to-your-user-model-optional)
- [Usage](#usage)
- [IP address lookup](#ip-address-lookup)
    - [Ip2Location Lite DB3](#ip2location-lite-db3)
    - [Custom provider](#custom-provider)
    - [Handle API errors](#handle-api-errors)
- [Events](#events)
    - [PersonalAccessTokenCreated](#personalaccesstokencreated)
- [License](#license)

Compatibility
-------------

[](#compatibility)

For recent Laravel versions, use [Laravel Logins](https://github.com/alajusticia/laravel-logins).

This package has been tested with **Laravel 8.x** and **Laravel Sanctum (v2)**.

Installation
------------

[](#installation)

Install the package with composer:

```
composer require alajusticia/laravel-sanctum-tracker
```

Publish the configuration file (`config/sanctum_tracker.php`) with:

```
php artisan vendor:publish --provider="ALajusticia\SanctumTracker\SanctumTrackerServiceProvider" --tag="config"
```

Run the migrations to update the `personal_access_tokens` table:

```
php artisan migrate
```

### Override default model

[](#override-default-model)

This package comes with a custom model (`ALajusticia\SanctumTracker\Models\PersonalAccessToken`) that extends the default Sanctum model.

Instruct Sanctum to use this custom model via the `usePersonalAccessTokenModel` method provided by Sanctum. Typically, you should call this method in the `boot` method of one of your application's service providers:

```
use ALajusticia\SanctumTracker\Models\PersonalAccessToken;
use Laravel\Sanctum\Sanctum;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
}
```

### Choose and install a user-agent parser

[](#choose-and-install-a-user-agent-parser)

This package relies on a User-Agent parser to extract the informations.

Currently, it supports two of the most popular parsers:

- WhichBrowser ()
- Agent ()

Before using the Sanctum Tracker, you need to choose a supported parser, install it and indicate in the configuration file which one you want to use.

### Add the trait to your user model (optional)

[](#add-the-trait-to-your-user-model-optional)

This package provides a `ALajusticia\SanctumTracker\Traits\SanctumTracked` trait that can be used on your user model to quickly revoke Sanctum tokens.

It introduces convenient methods:

- `logout`: to revoke the current token or a specific token by passing its ID in parameter
- `logoutOthers`: to revoke all the tokens, except the current one
- `logoutAll`: to revoke all the tokens, including the current one

```
use ALajusticia\SanctumTracker\Traits\SanctumTracked;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SanctumTracked;

    // ...
}
```

Usage
-----

[](#usage)

Issue Sanctum tokens like you would normally do. The `PersonalAccessToken` model provided by this package will automatically be populated with the extra informations.

IP address lookup
-----------------

[](#ip-address-lookup)

By default, the Sanctum Tracker collects the IP address and the informations given by the User-Agent header.

But you can go even further and collect other informations about the IP address, like the geolocation.

To do so, you first have to enable the IP lookup feature in the configuration file.

This package comes with two officially supported providers for IP address lookup (see the IP Address Lookup section in the `config/sanctum_tracker.php` configuration file).

### Ip2Location Lite DB3

[](#ip2location-lite-db3)

This package officially support the IP address geolocation with the Ip2Location Lite DB3.

Here are the steps to enable and use it:

- Download the current version of the database and import it in your database as explained in the documentation:
- Set the name of the `ip_lookup.provider` option to `ip2location-lite` in the `config/sanctum_tracker.php` configuration file
- Indicate the name of the tables used in your database for IPv4 and IPv6 in the `config/sanctum_tracker.php` configuration file (by default it uses the same names as the documentation: `ip2location_db3` and `ip2location_db3_ipv6`)

### Custom provider

[](#custom-provider)

You can add your own providers by creating a class that implements the `ALajusticia\SanctumTracker\Interfaces\IpProvider` interface and use the `ALajusticia\SanctumTracker\Traits\MakesApiCalls` trait.

Your custom class have to be registered in the `custom_providers` array of the configuration file.

Let's see an example of an IP lookup provider with the built-in `IpApi` provider:

```
use ALajusticia\SanctumTracker\Interfaces\IpProvider;
use ALajusticia\SanctumTracker\Traits\MakesApiCalls;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Illuminate\Support\Facades\Request;

class IpApi implements IpProvider
{
    use MakesApiCalls;

    /**
     * Get the Guzzle request.
     *
     * @return GuzzleRequest
     */
    public function getRequest()
    {
        return new GuzzleRequest('GET', 'http://ip-api.com/json/'.Request::ip().'?fields=25');
    }

    /**
     * Get the country name.
     *
     * @return string
     */
    public function getCountry()
    {
        return $this->result->get('country');
    }

    /**
     * Get the region name.
     *
     * @return string
     */
    public function getRegion()
    {
        return $this->result->get('regionName');
    }

    /**
     * Get the city name.
     *
     * @return string
     */
    public function getCity()
    {
        return $this->result->get('city');
    }
}
```

As you can see, the class has a `getRequest` method that must return a `GuzzleHttp\Psr7\Request` instance.

Guzzle utilizes PSR-7 as the HTTP message interface. Check its documentation:

The `IpProvider` interface comes with required methods related to the geolocation. All keys of the API response are accessible in your provider via `$this->result`, which is a Laravel collection.

If you want to collect other informations, you can add a `getCustomData` method in your custom provider. This custom data will be saved in the logins table in the `ip_data` JSON column. Let's see an example of additional data:

```
public function getCustomData()
{
    return [
        'country_code' => $this->result->get('countryCode'),
        'latitude' => $this->result->get('lat'),
        'longitude' => $this->result->get('lon'),
        'timezone' => $this->result->get('timezone'),
        'isp_name' => $this->result->get('isp'),
    ];
}
```

### Handle API errors

[](#handle-api-errors)

In case of an exception throwed during the API call of your IP address lookup provider, the FailedApiCall event is fired by this package.

This event has an exception attribute containing the GuzzleHttp\\Exception\\TransferException (see [Guzzle documentation](http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions)).

You can listen to this event to add your own logic.

Events
------

[](#events)

### PersonalAccessTokenCreated

[](#personalaccesstokencreated)

On a new login, you can listen to the `ALajusticia\SanctumTracker\Events\PersonalAccessTokenCreated` event. It has a `personalAccessToken` property containing the newly created `ALajusticia\SanctumTracker\Models\PersonalAccessToken` and a `context` property that receives a `ALajusticia\SanctumTracker\RequestContext` object containing all the informations collected on the request.

Available properties:

```
$this->context->userAgent; // The full, unparsed, User-Agent header
$this->context->ip; // The IP address
```

Available methods:

```
$this->context->parser(); // Returns the parser used to parse the User-Agent header
$this->context->ip(); // Returns the IP address lookup provider
```

Available methods in the parser:

```
$this->context->parser()->getDevice(); // The name of the device (MacBook...)
$this->context->parser()->getDeviceType(); // The type of the device (desktop, mobile, tablet, phone...)
$this->context->parser()->getPlatform(); // The name of the platform (macOS...)
$this->context->parser()->getBrowser(); // The name of the browser (Chrome...)
```

Available methods in the IP address lookup provider:

```
$this->context->ip()->getCountry(); // The name of the country
$this->context->ip()->getRegion(); // The name of the region
$this->context->ip()->getCity(); // The name of the city
$this->context->ip()->getResult(); // The entire result of the API call as a Laravel collection

// And all your custom methods in the case of a custom provider
```

License
-------

[](#license)

Open source, licensed under the [MIT license](LICENSE).

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 96.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~181 days

Recently: every ~226 days

Total

6

Last Release

1006d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9f22fbac3be34391e07c95c8400fa6ffca40fcec6792bbd5efb1cfb12050a3f8?d=identicon)[alajusticia](/maintainers/alajusticia)

---

Top Contributors

[![alajusticia](https://avatars.githubusercontent.com/u/17909018?v=4)](https://github.com/alajusticia "alajusticia (50 commits)")[![Tankonyako](https://avatars.githubusercontent.com/u/48120579?v=4)](https://github.com/Tankonyako "Tankonyako (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alajusticia-laravel-sanctum-tracker/health.svg)

```
[![Health](https://phpackages.com/badges/alajusticia-laravel-sanctum-tracker/health.svg)](https://phpackages.com/packages/alajusticia-laravel-sanctum-tracker)
```

###  Alternatives

[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k95.4M306](/packages/laravel-horizon)[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k15.1M132](/packages/laravel-pulse)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9762.4M131](/packages/roots-acorn)[flarum/core

Delightfully simple forum software.

201.4M2.3k](/packages/flarum-core)[fleetbase/core-api

Core Framework and Resources for Fleetbase API

1235.9k20](/packages/fleetbase-core-api)[hasinhayder/tyro

Tyro - The ultimate Authentication, Authorization, and Role &amp; Privilege Management solution for Laravel 12 &amp; 13

6804.7k6](/packages/hasinhayder-tyro)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
