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
$jwtToken = $query['jwt'] ?? ''; // JWT Token for authentication
if (!$driverId || empty($jwtToken)) {
logMsg("🚫 Connection Rejected: Missing driver_id or jwt token");
if (!$driverId) {
logMsg("🚫 Connection Rejected: Missing driver_id");
$socket->disconnect();
return;
}
try {
$secretKey = getJwtSecret();
if (empty($secretKey)) {
logMsg("⚠️ JWT Secret is not configured on the server!");
} else {
$decoded = JWT::decode($jwtToken, new Key($secretKey, 'HS256'));
// Validate that the token belongs to this driver
if ((string)$decoded->sub !== (string)$driverId || $decoded->role !== 'driver') {
logMsg("🚫 Connection Rejected: Invalid JWT for driver_id=$driverId");
$socket->disconnect();
return;
if (!empty($jwtToken)) {
try {
$secretKey = getJwtSecret();
if (empty($secretKey)) {
logMsg("⚠️ JWT Secret is not configured on the server!");
} else {
$decoded = JWT::decode($jwtToken, new Key($secretKey, 'HS256'));
// Validate that the token belongs to this driver
if ((string)$decoded->sub !== (string)$driverId || $decoded->role !== 'driver') {
logMsg("🚫 Connection Rejected: Invalid JWT for driver_id=$driverId");
$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);