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.
Laravel Devices provides extensive configuration options through the config/devices.php file. This guide covers all available options and their implications.
return [
// Device Configuration
'device_id_cookie_name' => 'device_id',
'device_id_storable_class' => DeviceId::class,
'regenerate_devices' => false,
// Session Configuration
'session_id_storable_class' => SessionId::class,
'allow_device_multi_session' => true,
'start_new_session_on_login' => false,
// Fingerprinting Configuration
'fingerprinting_enabled' => true,
'client_fingerprint_transport' => 'header',
'client_fingerprint_key' => 'X-Device-Fingerprint',
// Session Behavior
'inactivity_seconds' => 1200,
'inactivity_session_behaviour' => 'terminate',
// Cache Configuration
'cache_enabled_for' => ['device', 'location', 'session', 'ua'],
'cache_store' => 'redis',
'cache_ttl' => [...],
// Authentication & 2FA
'google_2fa_enabled' => true,
'google_2fa_window' => 1,
'google_2fa_qr_format' => 'base64',
// Routes & Redirects
'use_redirects' => true,
'load_routes' => true,
'auth_guard' => 'web',
'auth_middleware' => 'auth',
];
'device_id_cookie_name' => 'device_id'
'device_id''device_id_storable_class' => DeviceId::class
StorableId interface for device identificationNinja\DeviceTracker\ValueObject\DeviceId'regenerate_devices' => false
true, missing devices with valid cookies will be regeneratedfalse, missing devices throw DeviceNotFoundExceptionfalse for production environments'session_id_storable_class' => SessionId::class
StorableId interface for session identificationNinja\DeviceTracker\ValueObject\SessionId'allow_device_multi_session' => true
false, new sessions terminate existing ones// When allow_device_multi_session is false
$device = DeviceManager::current();
// First login - Creates session A
SessionManager::start(); // Session A active
// Second login - Session A is terminated, Session B created
SessionManager::start(); // Only Session B active
'start_new_session_on_login' => false
true, always creates new sessionfalse, refreshes existing session if available'fingerprinting_enabled' => true
'client_fingerprint_transport' => 'header'
'client_fingerprint_key' => 'X-Device-Fingerprint'
'header' or 'cookie'// Frontend
fetch('/api/endpoint', {
headers: {
'X-Device-Fingerprint': 'generated-fingerprint-value'
}
});
// Frontend
document.cookie = 'X-Device-Fingerprint=generated-fingerprint-value';
'inactivity_seconds' => 1200
0 to disable inactivity checking'inactivity_session_behaviour' => 'terminate'
'terminate' or 'ignore'// With 'terminate':
if ($session->inactive()) {
$session->end(); // Session is terminated
// User needs to login again
}
// With 'ignore':
if ($session->inactive()) {
// Session marked inactive but still valid
// Can be reactivated by user activity
$session->renew();
}
'cache_enabled_for' => ['device', 'location', 'session', 'ua']
'cache_store' => 'redis'
'redis' for production'cache_ttl' => [
'session' => 3600, // 1 hour
'device' => 3600, // 1 hour
'location' => 2592000, // 30 days
'ua' => 2592000, // 30 days
]
'google_2fa_enabled' => true
'google_2fa_window' => 1
'google_2fa_qr_format' => 'base64'
'base64' or 'svg''use_redirects' => true
true: Uses redirects for web routesfalse: Returns JSON responses'load_routes' => true
false to define routes manually'auth_guard' => 'web'
'auth_middleware' => 'auth'
'development_ip_pool' => [
'138.100.56.25',
'2.153.101.169',
// ...
],
'development_ua_pool' => [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...',
// ...
]
app()->environment('local')return [
'regenerate_devices' => false,
'allow_device_multi_session' => false,
'inactivity_seconds' => 600,
'inactivity_session_behaviour' => 'terminate',
'google_2fa_enabled' => true,
'google_2fa_window' => 1,
'fingerprinting_enabled' => true,
];
return [
'regenerate_devices' => true,
'allow_device_multi_session' => true,
'inactivity_seconds' => 0,
'google_2fa_enabled' => false,
'fingerprinting_enabled' => true,
'cache_store' => 'array',
];
return [
'use_redirects' => false,
'load_routes' => true,
'auth_guard' => 'api',
'auth_middleware' => 'auth:api',
'client_fingerprint_transport' => 'header',
];
How can I help you explore Laravel packages today?