Allow email and ID login in Admin auth login.php and bind web device fingerprint
This commit is contained in:
@@ -54,13 +54,21 @@ try {
|
|||||||
$stmt->execute([':fp' => $fpHash]);
|
$stmt->execute([':fp' => $fpHash]);
|
||||||
$admin = $stmt->fetch(PDO::FETCH_ASSOC);
|
$admin = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
// إذا لم يتم العثور بالبصمة، وتم تمرير رقم الهاتف (تسجيل دخول لأول مرة أو جهاز جديد)
|
// إذا لم يتم العثور بالبصمة، وتم تمرير اسم المستخدم/الإيميل/الهاتف (تسجيل دخول لأول مرة أو جهاز جديد)
|
||||||
if (!$admin && !empty($phone)) {
|
if (!$admin && !empty($phone)) {
|
||||||
|
// 1. تجربة البحث بالهاتف المشفّر
|
||||||
$encPhoneInput = $encryptionHelper->encryptData($phone);
|
$encPhoneInput = $encryptionHelper->encryptData($phone);
|
||||||
$stmtPhone = $con->prepare("SELECT * FROM adminUser WHERE phone = :phone LIMIT 1");
|
$stmtPhone = $con->prepare("SELECT * FROM adminUser WHERE phone = :phone LIMIT 1");
|
||||||
$stmtPhone->execute([':phone' => $encPhoneInput]);
|
$stmtPhone->execute([':phone' => $encPhoneInput]);
|
||||||
$admin = $stmtPhone->fetch(PDO::FETCH_ASSOC);
|
$admin = $stmtPhone->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
|
// 2. إذا لم يجد بالهاتف المشفّر، نجرب بالبريد الإلكتروني أو المعرّف (id / email) أو رقم الهاتف غير المشفّر
|
||||||
|
if (!$admin) {
|
||||||
|
$stmtAlt = $con->prepare("SELECT * FROM adminUser WHERE email = :input OR id = :input OR phone = :input LIMIT 1");
|
||||||
|
$stmtAlt->execute([':input' => $phone]);
|
||||||
|
$admin = $stmtAlt->fetch(PDO::FETCH_ASSOC);
|
||||||
|
}
|
||||||
|
|
||||||
// تأكيد كلمة المرور وتحديث بصمة الجهاز إذا تم إيجاد الحساب
|
// تأكيد كلمة المرور وتحديث بصمة الجهاز إذا تم إيجاد الحساب
|
||||||
if ($admin && password_verify($password, $admin['password'])) {
|
if ($admin && password_verify($password, $admin['password'])) {
|
||||||
$encFpRaw = $encryptionHelper->encryptData($fingerprint);
|
$encFpRaw = $encryptionHelper->encryptData($fingerprint);
|
||||||
|
|||||||
@@ -125,22 +125,49 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await response.json();
|
const resText = await response.text();
|
||||||
|
console.log('Server Raw Response:', resText);
|
||||||
|
|
||||||
if (response.ok && (res.status === 'success' || res.jwt || res.data?.jwt)) {
|
let res;
|
||||||
const jwtToken = res.jwt || res.data?.jwt;
|
try {
|
||||||
const adminInfo = res.admin || res.data?.admin || {};
|
res = JSON.parse(resText);
|
||||||
currentUser = {
|
} catch (jsonErr) {
|
||||||
name: adminInfo.name || 'Admin',
|
showNotification(`Server Error (${response.status}): ${resText.substring(0, 100)}`, 'danger');
|
||||||
email: adminInfo.email || phone,
|
return;
|
||||||
role: adminInfo.role || 'Administrator',
|
}
|
||||||
jwt: jwtToken,
|
|
||||||
isLive: true
|
console.log('Parsed API Response:', res);
|
||||||
};
|
|
||||||
localStorage.setItem('siro_admin_user', JSON.stringify(currentUser));
|
// Check if login succeeded and JWT was returned
|
||||||
authWrapper.classList.add('hidden');
|
const jwtToken = res.jwt || res.data?.jwt || (typeof res.message === 'object' ? res.message?.jwt : null);
|
||||||
showNotification('Access Granted! Welcome to Admin Portal.', 'success');
|
const adminInfo = res.admin || res.data?.admin || (typeof res.message === 'object' ? res.message?.admin : {}) || {};
|
||||||
fetchLiveDashboardData();
|
|
||||||
|
if (response.ok && (res.status === 'success' || jwtToken)) {
|
||||||
|
if (jwtToken) {
|
||||||
|
currentUser = {
|
||||||
|
name: adminInfo.name || 'Admin',
|
||||||
|
email: adminInfo.email || phone,
|
||||||
|
role: adminInfo.role || 'Administrator',
|
||||||
|
jwt: jwtToken,
|
||||||
|
isLive: true
|
||||||
|
};
|
||||||
|
localStorage.setItem('siro_admin_user', JSON.stringify(currentUser));
|
||||||
|
authWrapper.classList.add('hidden');
|
||||||
|
showNotification('Access Granted! Welcome to Admin Portal.', 'success');
|
||||||
|
fetchLiveDashboardData();
|
||||||
|
} else if (res.status === 'otp_required' || (res.message && (res.message.status === 'otp_required' || res.message === 'otp_required'))) {
|
||||||
|
pendingOtpPhone = phone;
|
||||||
|
pendingOtpPassword = password;
|
||||||
|
const masked = res.phone || (typeof res.message === 'object' ? res.message.phone : null) || phone;
|
||||||
|
document.getElementById('otpPhoneText').textContent = `Verification code sent to WhatsApp (${masked})`;
|
||||||
|
document.getElementById('otpModal').classList.add('active');
|
||||||
|
} else {
|
||||||
|
showNotification('Login successful, loading dashboard...', 'success');
|
||||||
|
currentUser = { name: adminInfo.name || 'Admin', email: phone, role: 'Administrator', isLive: true };
|
||||||
|
localStorage.setItem('siro_admin_user', JSON.stringify(currentUser));
|
||||||
|
authWrapper.classList.add('hidden');
|
||||||
|
fetchLiveDashboardData();
|
||||||
|
}
|
||||||
} else if (res.status === 'otp_required' || (res.message && (res.message.status === 'otp_required' || res.message === 'otp_required'))) {
|
} else if (res.status === 'otp_required' || (res.message && (res.message.status === 'otp_required' || res.message === 'otp_required'))) {
|
||||||
pendingOtpPhone = phone;
|
pendingOtpPhone = phone;
|
||||||
pendingOtpPassword = password;
|
pendingOtpPassword = password;
|
||||||
@@ -148,13 +175,13 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
document.getElementById('otpPhoneText').textContent = `Verification code sent to WhatsApp (${masked})`;
|
document.getElementById('otpPhoneText').textContent = `Verification code sent to WhatsApp (${masked})`;
|
||||||
document.getElementById('otpModal').classList.add('active');
|
document.getElementById('otpModal').classList.add('active');
|
||||||
} else {
|
} else {
|
||||||
// Show REAL backend API error message and DO NOT redirect to demo mode
|
// Show REAL backend API error message
|
||||||
const errorMsg = res.message || res.error || 'Invalid username or password.';
|
const errorMsg = (typeof res.message === 'string' ? res.message : null) || res.error || 'Invalid credentials or user not found.';
|
||||||
showNotification(`Login Error: ${errorMsg}`, 'danger');
|
showNotification(`Login Error: ${errorMsg}`, 'danger');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error('Login Exception:', err);
|
||||||
showNotification('Connection Error: Could not reach authentication server.', 'danger');
|
showNotification(`Network Error: ${err.message || 'Could not connect to backend'}`, 'danger');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
currentUser = { name: 'Super Admin (Demo)', email: phone, role: 'Administrator', isLive: false };
|
currentUser = { name: 'Super Admin (Demo)', email: phone, role: 'Administrator', isLive: false };
|
||||||
|
|||||||
Reference in New Issue
Block a user