Restore WhatsApp OTP (Nabeh API) flow for Admin authentication

This commit is contained in:
Hamza-Ayed
2026-07-25 00:25:53 +03:00
parent 4c738e8f43
commit 9d2a665d64
+51 -15
View File
@@ -109,29 +109,65 @@ try {
// 2. التحقق من كلمة المرور // 2. التحقق من كلمة المرور
if (password_verify($password, $admin['password'])) { if (password_verify($password, $admin['password'])) {
// إصدار JWT مباشرة عند نجاح كلمة المرور
$jwtService = new JwtService($redis);
$role = $admin['role'] ?? 'admin';
if ($redis) { // إذا كان تجديد توكن تلقائي من التطبيق/الجهاز الموثوق
$oldJti = $redis->get("active_jti:" . $admin['id']); if ($isRenewal) {
if ($oldJti) { $jwtService = new JwtService($redis);
$jwtService->revokeToken($oldJti, 3600); $role = $admin['role'] ?? 'admin';
if ($redis) {
$oldJti = $redis->get("active_jti:" . $admin['id']);
if ($oldJti) {
$jwtService->revokeToken($oldJti, 3600);
}
} }
$jwt = $jwtService->generateAccessToken($admin['id'], $role, $audience, $fingerprint);
if ($encryptionHelper && !empty($admin['name'])) {
$admin['name'] = $encryptionHelper->decryptData($admin['name']) ?: $admin['name'];
}
unset($admin['password']);
printSuccess([
"message" => "Login successful",
"admin" => $admin,
"jwt" => $jwt,
"expires_in" => 3600
]);
exit;
} }
$jwt = $jwtService->generateAccessToken($admin['id'], $role, $audience, $fingerprint); // 3. توليد رمز تحقق OTP (3 أرقام) وإرساله عبر نظام OTP الموحد (Nabeh API للواتساب)
$otp = (string)random_int(100, 999);
$encryptedPhone = $admin['phone'] ?? '';
if ($encryptionHelper && !empty($admin['name'])) { $rawPhone = ($encryptionHelper && !empty($encryptedPhone)) ? $encryptionHelper->decryptData($encryptedPhone) : $encryptedPhone;
$admin['name'] = $encryptionHelper->decryptData($admin['name']) ?: $admin['name']; if (!$rawPhone || empty($rawPhone)) {
$rawPhone = $encryptedPhone;
} }
unset($admin['password']);
// تحميل موزع خدمات OTP عبر Nabeh API
require_once __DIR__ . '/../../auth/otp/providers.php';
$success = false;
if (function_exists('sendNabehOtp')) {
$success = sendNabehOtp($rawPhone, $otp, 'whatsapp', 'admin');
}
// تخزين OTP (SHA-256 hash) في جدول token_verification_admin
$otpHash = hash('sha256', $otp);
$stmt = $con->prepare("INSERT INTO token_verification_admin (phone_number, token, expiration_time)
VALUES (?, ?, DATE_ADD(NOW(), INTERVAL 10 MINUTE))
ON DUPLICATE KEY UPDATE token = VALUES(token), expiration_time = VALUES(expiration_time)");
$stmt->execute([$encryptedPhone, $otpHash]);
$maskedPhone = (strlen($rawPhone) > 7) ? substr($rawPhone, 0, 4) . '****' . substr($rawPhone, -3) : $rawPhone;
printSuccess([ printSuccess([
"message" => "Login successful", "status" => "otp_required",
"admin" => $admin, "message" => $success ? "تم إرسال رمز التحقق إلى WhatsApp الخاص بك." : "فشل إرسال واتساب. تحقق من error_log لمعرفة OTP.",
"jwt" => $jwt, "phone" => $maskedPhone
"expires_in" => 3600
]); ]);
exit; exit;
} else { } else {