init backend presensi
This commit is contained in:
66
app/Modules/Devices/Services/DeviceAuthService.php
Normal file
66
app/Modules/Devices/Services/DeviceAuthService.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Devices\Services;
|
||||
|
||||
use App\Modules\Devices\Models\DeviceModel;
|
||||
|
||||
/**
|
||||
* Device Authentication Service
|
||||
*
|
||||
* Handles device authentication logic.
|
||||
*/
|
||||
class DeviceAuthService
|
||||
{
|
||||
protected DeviceModel $deviceModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->deviceModel = new DeviceModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate device by device_code and api_key
|
||||
*
|
||||
* @param string $deviceCode Device code
|
||||
* @param string $apiKey API key
|
||||
* @return array|null Returns device data if authenticated, null otherwise
|
||||
*/
|
||||
public function authenticate(string $deviceCode, string $apiKey): ?array
|
||||
{
|
||||
// Find active device by device_code
|
||||
$device = $this->deviceModel->findActiveByDeviceCode($deviceCode);
|
||||
|
||||
if (!$device) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// For development: compare plain api_key
|
||||
// In production, should use password_verify() with hashed api_key
|
||||
if ($device->api_key !== $apiKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Update last_seen_at
|
||||
$this->touchLastSeen($device->id);
|
||||
|
||||
return [
|
||||
'device_id' => $device->id,
|
||||
'device_code' => $device->device_code,
|
||||
'device_name' => $device->device_name,
|
||||
'latitude' => $device->latitude !== null ? (float) $device->latitude : null,
|
||||
'longitude' => $device->longitude !== null ? (float) $device->longitude : null,
|
||||
'radius_meters' => $device->radius_meters !== null ? (int) $device->radius_meters : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update last_seen_at timestamp for device
|
||||
*
|
||||
* @param int $deviceId Device ID
|
||||
* @return void
|
||||
*/
|
||||
public function touchLastSeen(int $deviceId): void
|
||||
{
|
||||
$this->deviceModel->updateLastSeen($deviceId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user