diego-ninja/laravel-devices
Laravel package for tracking authenticated user devices and managing sessions. Includes device verification, fingerprinting integrations, session locking/blocking with optional Google 2FA, location tracking, events, middleware/controllers, and caching support.
Before installing Laravel Devices, ensure your environment meets the following requirements:
composer require diego-ninja/laravel-devices
php artisan vendor:publish --provider="Ninja\DeviceTracker\DeviceTrackerServiceProvider"
This command will publish:
config/devices.phpdatabase/migrations/:
php artisan migrate
Add the necessary traits to your User model:
use Ninja\DeviceTracker\Traits\HasDevices;
use Ninja\DeviceTracker\Traits\Has2FA; // Optional, only if using 2FA
class User extends Authenticatable
{
use HasDevices;
use Has2FA; // Optional
// ... rest of your model
}
Add the required middleware in your boot/app.php:
protected $middleware = [
// ... other middleware
\Ninja\DeviceTracker\Http\Middleware\DeviceTracker::class,
\Ninja\DeviceTracker\Modules\Fingerprinting\Http\Middleware\FingerprintTracker::class,
];
protected $routeMiddleware = [
// ... other route middleware
'session-tracker' => \Ninja\DeviceTracker\Http\Middleware\SessionTracker::class,
];
If you're using Laravel < 10, add the service provider to config/app.php:
'providers' => [
// ... other providers
Ninja\DeviceTracker\DeviceTrackerServiceProvider::class,
],
For Laravel 10+ using package discovery, this step is not necessary.
For optimal performance, configure a fast cache driver in your .env file:
CACHE_DRIVER=redis
REDIS_CLIENT=predis
If you plan to use Google 2FA:
Has2FA trait// config/devices.php
return [
'google_2fa_enabled' => true,
'google_2fa_company' => env('APP_NAME', 'Your Company'),
];
Choose your preferred fingerprinting library:
// config/devices.php
return [
'fingerprinting_enabled' => true,
'client_fingerprint_transport' => 'header', // or 'cookie'
'client_fingerprint_key' => 'X-Device-Fingerprint',
];
Define your preferred session handling behavior:
// config/devices.php
return [
'allow_device_multi_session' => true,
'start_new_session_on_login' => false,
'inactivity_seconds' => 1200, // 20 minutes
'inactivity_session_behaviour' => 'terminate', // or 'ignore'
];
To verify your installation is working correctly:
php artisan migrate:status
use Ninja\DeviceTracker\Facades\DeviceManager;
// In a route or controller:
if (DeviceManager::tracked()) {
$device = DeviceManager::current();
return "Device tracked successfully: " . $device->uuid;
}
Class not found errors
composer dump-autoloadMigration errors
php artisan config:clearMiddleware not working
php artisan route:clearEnable debug mode in your configuration for more detailed error messages:
// config/devices.php
return [
'debug' => true,
// ... other config
];
Once installed, you should:
For more information on specific features:
How can I help you explore Laravel packages today?