🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.
Device Management
The package uses device fingerprinting to reliably identify and manage user devices. Each device is assigned a unique fingerprint based on browser characteristics, IP address, and other factors.
Device Fingerprinting
Device fingerprinting uses SHA-256 hashing to create a unique identifier for each device. This is more reliable than using just IP address and user agent, as it accounts for multiple factors.
How It Works
When a user logs in, the package automatically:
- Generates a device fingerprint using browser characteristics
- Creates a friendly device name (e.g., "Chrome on Windows")
- Checks if the device has been seen before
- Stores the device information with the authentication log
Getting User Devices
Get all devices that have been used by a user:
1$user = User::find(1); 2$devices = $user->getDevices(); 3 4// Returns a collection with: 5// - device_id 6// - device_name 7// - ip_address 8// - user_agent 9// - is_trusted10// - login_at (most recent)
Trusting Devices
Mark a device as trusted to allow it access to sensitive actions:
1$user = User::find(1); 2$deviceId = 'abc123...'; // Device fingerprint 3 4// Trust a device 5$user->trustDevice($deviceId); 6 7// Check if device is trusted 8if ($user->isDeviceTrusted($deviceId)) { 9 // Device is trusted10}
Untrusting Devices
Remove trust from a device:
1$user = User::find(1);2$deviceId = 'abc123...';3 4$user->untrustDevice($deviceId);
Updating Device Names
Allow users to customize device names for easier identification:
1$user = User::find(1);2$deviceId = 'abc123...';3 4$user->updateDeviceName($deviceId, 'My Work Laptop');
Example: Device Management UI
1// In your controller 2public function devices() 3{ 4 $user = auth()->user(); 5 $devices = $user->getDevices(); 6 7 return view('profile.devices', compact('devices')); 8} 9 10// In your view11@foreach($devices as $device)12 <div class="device-item">13 <div>14 <strong>{{ $device->device_name }}</strong>15 @if($device->is_trusted)16 <span class="badge badge-success">Trusted</span>17 @endif18 </div>19 <div>20 Last used: {{ $device->login_at->diffForHumans() }}21 </div>22 <div>23 <form action="{{ route('devices.trust', $device->device_id) }}" method="POST">24 @csrf25 @if($device->is_trusted)26 <button type="submit" name="action" value="untrust">Untrust Device</button>27 @else28 <button type="submit" name="action" value="trust">Trust Device</button>29 @endif30 </form>31 </div>32 </div>33@endforeach
Getting Current Device ID
To get the current device's fingerprint:
1use Rappasoft\LaravelAuthenticationLog\Helpers\DeviceFingerprint;2 3$currentDeviceId = DeviceFingerprint::generate(request());4$deviceName = DeviceFingerprint::generateDeviceName(request());