Update: 2026-06-23 17:25:29

This commit is contained in:
Hamza-Ayed
2026-06-23 17:25:29 +03:00
parent 4e2f165d60
commit 148ca3af1d
5 changed files with 46 additions and 71 deletions

View File

@@ -52,8 +52,8 @@ function sendKazumiSms(string $receiver, string $otp): bool {
*/
function getNabehBearerToken(): ?string {
global $redis;
// 1. Try to read cached token from Redis (TTL 24 hours)
// 1. Try fetching from Redis first
if ($redis) {
try {
$cachedToken = $redis->get('nabeh_bearer_token');
@@ -61,16 +61,18 @@ function getNabehBearerToken(): ?string {
return $cachedToken;
}
} catch (Exception $e) {
error_log("⚠️ [Nabeh Auth Redis] Error reading token: " . $e->getMessage());
$msg = "⚠️ [Nabeh Auth Redis] Error reading token: " . $e->getMessage();
error_log($msg); echo $msg . "<br>";
}
}
// 2. Token not cached, authenticate via Nabeh Login API
$email = getenv('NABEH_EMAIL');
$password = getenv('NABEH_PASSWORD');
if (!$email || !$password) {
error_log("⚠️ [Nabeh Auth] Missing NABEH_EMAIL or NABEH_PASSWORD environment variables.");
$msg = "⚠️ [Nabeh Auth] Missing NABEH_EMAIL or NABEH_PASSWORD environment variables.";
error_log($msg); echo $msg . "<br>";
return null;
}
@@ -87,19 +89,24 @@ function getNabehBearerToken(): ?string {
if ($response) {
$decoded = json_decode($response, true);
$token = $decoded['token'] ?? $decoded['message']['token'] ?? $decoded['jwt'] ?? $decoded['access_token'] ?? null;
if ($token) {
// Cache token in Redis for 24 hours (86400 seconds)
// 3. Cache token in Redis for 24h
if ($redis) {
try {
$redis->setex('nabeh_bearer_token', 86400, $token);
} catch (Exception $e) {
error_log("⚠️ [Nabeh Auth Redis Cache Save] Error saving token: " . $e->getMessage());
$msg = "⚠️ [Nabeh Auth Redis Cache Save] Error saving token: " . $e->getMessage();
error_log($msg); echo $msg . "<br>";
}
}
return $token;
}
error_log("❌ [Nabeh Auth] Failed to extract token from login response: " . $response);
$msg = "❌ [Nabeh Auth] Failed to extract token from login response: " . $response;
error_log($msg); echo $msg . "<br>";
} else {
$msg = "❌ [Nabeh Auth] Empty response from login API cURL.";
error_log($msg); echo $msg . "<br>";
}
return null;
}
@@ -115,7 +122,8 @@ function getNabehBearerToken(): ?string {
function sendNabehOtp(string $receiver, string $otp, string $method = 'text'): bool {
$bearerToken = getNabehBearerToken();
if (!$bearerToken) {
error_log("⚠️ [Nabeh OTP] Failed to obtain dynamic JWT Bearer token.");
$msg = "⚠️ [Nabeh OTP] Failed to obtain dynamic JWT Bearer token.";
error_log($msg); echo $msg . "<br>";
return false;
}
@@ -129,9 +137,6 @@ function sendNabehOtp(string $receiver, string $otp, string $method = 'text'): b
} elseif ($method === 'image') {
$type = 'image';
}
// elseif ($method === 'flash_call') {
// $type = 'flash_call';
// }
$apiUrl = 'https://nabeh.intaleqapp.com/api/otp/send';
$payload = [
@@ -150,7 +155,11 @@ function sendNabehOtp(string $receiver, string $otp, string $method = 'text'): b
if ($decoded && ($decoded['success'] ?? false)) {
return true;
}
error_log("❌ [Nabeh OTP] API returned failure response: " . $response);
$msg = "❌ [Nabeh OTP] API returned failure response: " . $response;
error_log($msg); echo $msg . "<br>";
} else {
$msg = "❌ [Nabeh OTP] Empty response from cURL.";
error_log($msg); echo $msg . "<br>";
}
return false;
}
@@ -217,12 +226,14 @@ function curlCall(string $method, string $url, string $data, array $headers): ?s
curl_close($ch);
if ($error) {
error_log("⚠️ [OTP cURL] Error calling $url: $error");
$msg = "⚠️ [OTP cURL] Error calling $url: $error";
error_log($msg); echo $msg . "<br>";
return null;
}
if ($httpCode !== 200) {
error_log("⚠️ [OTP cURL] Non-200 HTTP code $httpCode from $url. Response: $response");
$msg = "⚠️ [OTP cURL] Non-200 HTTP code $httpCode from $url. Response: $response";
error_log($msg); echo $msg . "<br>";
}
return $response;

View File

@@ -2,6 +2,11 @@
// File: backend/auth/otp/request.php
// Unified OTP request endpoint with geographical routing (Syria, Egypt, Jordan)
// Enable error reporting for debug
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/../../core/bootstrap.php';
require_once __DIR__ . '/../../functions.php';
require_once __DIR__ . '/providers.php';