Implement device and IP tracking for users
Added Device and UserIp models and migrations to store device fingerprints and user IPs with expiration. Updated ShowSavingIp Livewire component and view to capture and persist both IP and device fingerprint. Introduced a scheduled command to clean expired user IPs and registered it in the console kernel. Removed the old migration for storing IPs directly on the users table.
This commit is contained in:
@@ -4,24 +4,53 @@ namespace App\Http\Livewire;
|
||||
|
||||
use Livewire\Component;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\UserIp;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Device;
|
||||
|
||||
class ShowSavingIp extends Component
|
||||
{
|
||||
public $ip_address;
|
||||
public $device_fingerprint;
|
||||
|
||||
protected $listeners = ['setIpAddress'];
|
||||
protected $listeners = ['setDeviceData'];
|
||||
|
||||
public function setIpAddress($ip)
|
||||
public function setDeviceData($data)
|
||||
{
|
||||
$this->ip_address = $ip;
|
||||
$this->ip_address = $data['ip'] ?? null;
|
||||
$this->device_fingerprint = $data['fingerprint'] ?? null;
|
||||
|
||||
$user = Auth::user();
|
||||
if ($user) {
|
||||
$user->ip_address = $ip;
|
||||
$user->save();
|
||||
if (!$user) return;
|
||||
|
||||
// Guardar IP pública temporal (12h)
|
||||
if ($this->ip_address) {
|
||||
UserIp::updateOrCreate(
|
||||
[
|
||||
'user_id' => $user->id,
|
||||
'ip_address' => $this->ip_address,
|
||||
],
|
||||
[
|
||||
'expires_at' => Carbon::now()->addHours(12),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// Guardar fingerprint permanente del dispositivo
|
||||
if ($this->device_fingerprint) {
|
||||
Device::updateOrCreate(
|
||||
[
|
||||
'fingerprint' => $this->device_fingerprint,
|
||||
],
|
||||
[
|
||||
'user_id' => $user->id,
|
||||
'last_seen_at' => now(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.show-saving-ip');
|
||||
|
||||
Reference in New Issue
Block a user