Download the PHP package alajusticia/laravel-sanctum-tracker without Composer
On this page you can find all versions of the php package alajusticia/laravel-sanctum-tracker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alajusticia/laravel-sanctum-tracker
More information about alajusticia/laravel-sanctum-tracker
Files in alajusticia/laravel-sanctum-tracker
Package laravel-sanctum-tracker
Short Description Track Sanctum tokens in Laravel.
License MIT
Informations about the package laravel-sanctum-tracker
Laravel Sanctum Tracker
⚠️ This package is now archived. Use Laravel Logins instead.
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
- Installation
- Override default model
- Choose and install a user-agent parser
- Add the trait to your user model (optional)
- Usage
- IP address lookup
- Ip2Location Lite DB3
- Custom provider
- Handle API errors
- Events
- PersonalAccessTokenCreated
- License
Compatibility
For recent Laravel versions, use Laravel Logins.
This package has been tested with Laravel 8.x and Laravel Sanctum (v2).
Installation
Install the package with composer:
Publish the configuration file (config/sanctum_tracker.php
) with:
Run the migrations to update the personal_access_tokens
table:
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:
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 (https://github.com/WhichBrowser/Parser-PHP)
- Agent (https://github.com/jenssegers/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)
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 parameterlogoutOthers
: to revoke all the tokens, except the current onelogoutAll
: to revoke all the tokens, including the current one
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
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
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: https://lite.ip2location.com/database/ip-country-region-city
-
Set the name of the
ip_lookup.provider
option toip2location-lite
in theconfig/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
andip2location_db3_ipv6
)
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:
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: http://docs.guzzlephp.org/en/stable/psr7.html
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:
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).
You can listen to this event to add your own logic.
Events
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:
Available methods:
Available methods in the parser:
Available methods in the IP address lookup provider:
License
Open source, licensed under the MIT license.
All versions of laravel-sanctum-tracker with dependencies
alajusticia/laravel-expirable Version ^1.5
guzzlehttp/guzzle Version ^7.3
illuminate/database Version ^7.0|^8.0
illuminate/queue Version ^7.0|^8.0
illuminate/support Version ^7.0|^8.0
laravel/sanctum Version ^2.9
nesbot/carbon Version ^2.53