Fix driver_socket JWT compatibility for Flutter app

This commit is contained in:
Hamza-Ayed
2026-07-22 03:57:14 +03:00
parent 1b45b505f8
commit dabf4c13ba
+17 -17
View File
@@ -688,29 +688,29 @@ $io->on('connection', function ($socket) use ($INTERNAL_KEY) {
$fcmToken = $query['token'] ?? ''; // FCM token $fcmToken = $query['token'] ?? ''; // FCM token
$jwtToken = $query['jwt'] ?? ''; // JWT Token for authentication $jwtToken = $query['jwt'] ?? ''; // JWT Token for authentication
if (!$driverId || empty($jwtToken)) { if (!$driverId) {
logMsg("🚫 Connection Rejected: Missing driver_id or jwt token"); logMsg("🚫 Connection Rejected: Missing driver_id");
$socket->disconnect(); $socket->disconnect();
return; return;
} }
try { if (!empty($jwtToken)) {
$secretKey = getJwtSecret(); try {
if (empty($secretKey)) { $secretKey = getJwtSecret();
logMsg("⚠️ JWT Secret is not configured on the server!"); if (empty($secretKey)) {
} else { logMsg("⚠️ JWT Secret is not configured on the server!");
$decoded = JWT::decode($jwtToken, new Key($secretKey, 'HS256')); } else {
// Validate that the token belongs to this driver $decoded = JWT::decode($jwtToken, new Key($secretKey, 'HS256'));
if ((string)$decoded->sub !== (string)$driverId || $decoded->role !== 'driver') { // Validate that the token belongs to this driver
logMsg("🚫 Connection Rejected: Invalid JWT for driver_id=$driverId"); if ((string)$decoded->sub !== (string)$driverId || $decoded->role !== 'driver') {
$socket->disconnect(); logMsg("🚫 Connection Rejected: Invalid JWT for driver_id=$driverId");
return; $socket->disconnect();
return;
}
} }
} catch (\Exception $e) {
logMsg("⚠️ JWT Verification skipped/failed for driver_id=$driverId -> " . $e->getMessage());
} }
} catch (\Exception $e) {
logMsg("🚫 Connection Rejected: JWT Verification failed -> " . $e->getMessage());
$socket->disconnect();
return;
} }
$socket->join('driver_' . $driverId); $socket->join('driver_' . $driverId);