Update: 2026-07-24 23:31:37
This commit is contained in:
@@ -102,49 +102,59 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const password = document.getElementById('loginPass').value.trim();
|
const password = document.getElementById('loginPass').value.trim();
|
||||||
|
|
||||||
if (isLiveMode) {
|
if (isLiveMode) {
|
||||||
showNotification('Authenticating device with server...', 'info');
|
showNotification('Authenticating credentials with server...', 'info');
|
||||||
try {
|
try {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('phone', phone);
|
formData.append('phone', phone);
|
||||||
|
formData.append('id', phone); // send both phone and id for maximum compatibility
|
||||||
formData.append('password', password);
|
formData.append('password', password);
|
||||||
formData.append('fingerprint', deviceFingerprint);
|
formData.append('fingerprint', deviceFingerprint);
|
||||||
formData.append('aud', 'admin');
|
formData.append('aud', 'admin');
|
||||||
|
|
||||||
const response = await fetch('/backend/Admin/auth/login.php', {
|
// Try Admin auth login endpoint
|
||||||
|
let response = await fetch('/backend/Admin/auth/login.php', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData
|
body: formData
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Fallback to loginAdmin.php if 404
|
||||||
|
if (response.status === 404) {
|
||||||
|
response = await fetch('/backend/loginAdmin.php', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const res = await response.json();
|
const res = await response.json();
|
||||||
if (response.ok && res.status === 'success') {
|
|
||||||
if (res.jwt) {
|
if (response.ok && (res.status === 'success' || res.jwt || res.data?.jwt)) {
|
||||||
currentUser = {
|
const jwtToken = res.jwt || res.data?.jwt;
|
||||||
name: res.admin?.name || 'Admin',
|
const adminInfo = res.admin || res.data?.admin || {};
|
||||||
email: res.admin?.email || phone,
|
currentUser = {
|
||||||
role: res.admin?.role || 'Administrator',
|
name: adminInfo.name || 'Admin',
|
||||||
jwt: res.jwt,
|
email: adminInfo.email || phone,
|
||||||
isLive: true
|
role: adminInfo.role || 'Administrator',
|
||||||
};
|
jwt: jwtToken,
|
||||||
localStorage.setItem('siro_admin_user', JSON.stringify(currentUser));
|
isLive: true
|
||||||
authWrapper.classList.add('hidden');
|
};
|
||||||
showNotification('Access Granted! Device Fingerprint Verified.', 'success');
|
localStorage.setItem('siro_admin_user', JSON.stringify(currentUser));
|
||||||
fetchLiveDashboardData();
|
authWrapper.classList.add('hidden');
|
||||||
} else if (res.message && res.message.status === 'otp_required' || res.status === 'otp_required') {
|
showNotification('Access Granted! Welcome to Admin Portal.', 'success');
|
||||||
pendingOtpPhone = phone;
|
fetchLiveDashboardData();
|
||||||
pendingOtpPassword = password;
|
} else if (res.status === 'otp_required' || (res.message && (res.message.status === 'otp_required' || res.message === 'otp_required'))) {
|
||||||
const masked = res.phone || res.message?.phone || phone;
|
pendingOtpPhone = phone;
|
||||||
document.getElementById('otpPhoneText').textContent = `Verification code sent to WhatsApp (${masked})`;
|
pendingOtpPassword = password;
|
||||||
document.getElementById('otpModal').classList.add('active');
|
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 {
|
} else {
|
||||||
showNotification(res.message || 'Invalid credentials or device not registered.', 'warning');
|
// Show REAL backend API error message and DO NOT redirect to demo mode
|
||||||
|
const errorMsg = res.message || res.error || 'Invalid username or password.';
|
||||||
|
showNotification(`Login Error: ${errorMsg}`, 'danger');
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
showNotification('Cannot connect to Live Backend. Entering Demo Mode.', 'info');
|
showNotification('Connection Error: Could not reach authentication server.', 'danger');
|
||||||
currentUser = { name: 'Super Admin (Demo)', email: phone, role: 'Administrator', isLive: false };
|
|
||||||
localStorage.setItem('siro_admin_user', JSON.stringify(currentUser));
|
|
||||||
authWrapper.classList.add('hidden');
|
|
||||||
}
|
}
|
||||||
} 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