69 lines
2.2 KiB
HTML
69 lines
2.2 KiB
HTML
<script>
|
|
const API_URL = '../api.php';
|
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
// 1. Vérifier la sécurité
|
|
try {
|
|
const res = await fetch(`${API_URL}?action=check_security_status`);
|
|
const data = await res.json();
|
|
console.log("Statut serveur :", data);
|
|
|
|
const formBlock = document.getElementById('login-form-block');
|
|
const blankBlock = document.getElementById('blank-setup-block');
|
|
|
|
if (data.is_blank === true) {
|
|
formBlock.style.display = 'none';
|
|
blankBlock.style.display = 'block';
|
|
} else {
|
|
formBlock.style.display = 'block';
|
|
blankBlock.style.display = 'none';
|
|
}
|
|
} catch(e) {
|
|
alert("Erreur de communication avec api.php : " + e.message);
|
|
}
|
|
});
|
|
|
|
async function doLoginBlank() {
|
|
await performLogin(""); // Envoie un mot de passe vide
|
|
}
|
|
|
|
async function doLogin() {
|
|
const pwd = document.getElementById('login-pwd').value;
|
|
await performLogin(pwd);
|
|
}
|
|
|
|
async function performLogin(pwd) {
|
|
try {
|
|
const res = await fetch(`${API_URL}?action=login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ password: pwd })
|
|
});
|
|
|
|
const data = await res.json();
|
|
console.log("Réponse login :", data);
|
|
|
|
if (data.success) {
|
|
localStorage.setItem('token', data.token);
|
|
|
|
// Si blank, on force la création du mot de passe
|
|
if (data.blank === true) {
|
|
const newPwd = prompt("Système initialisé. Définissez votre mot de passe (min 4 caractères) :");
|
|
if (newPwd && newPwd.length >= 4) {
|
|
await fetch(`${API_URL}?action=setup_admin`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'Authorization': data.token },
|
|
body: JSON.stringify({ password: newPwd })
|
|
});
|
|
alert("Mot de passe enregistré !");
|
|
}
|
|
}
|
|
window.location.href = 'dashboard.html';
|
|
} else {
|
|
alert("Erreur : " + (data.error || "Mot de passe incorrect"));
|
|
}
|
|
} catch(e) {
|
|
alert("Erreur technique : " + e.message);
|
|
}
|
|
}
|
|
</script> |