Actualiser admin/login.html
This commit is contained in:
+81
-67
@@ -1,73 +1,87 @@
|
|||||||
<script>
|
<!DOCTYPE html>
|
||||||
const API_URL = '../api.php';
|
<html lang="fr">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Connexion Backoffice</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: sans-serif; display: flex; justify-content: center; padding-top: 50px; }
|
||||||
|
.login-wrap { border: 1px solid #ccc; padding: 20px; border-radius: 8px; width: 300px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', async () => {
|
<div class="login-wrap">
|
||||||
const formBlock = document.getElementById('login-form-block');
|
<h2>Backoffice</h2>
|
||||||
const blankBlock = document.getElementById('blank-setup-block');
|
|
||||||
|
|
||||||
// Vérification de sécurité pour éviter l'erreur "null"
|
<div id="login-form-block" style="display:none;">
|
||||||
if (!formBlock || !blankBlock) {
|
<input type="password" id="login-pwd" placeholder="Mot de passe">
|
||||||
console.error("Erreur : Un des blocs de connexion est introuvable dans le HTML.");
|
<button onclick="doLogin()">Accéder</button>
|
||||||
return;
|
</div>
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
<div id="blank-setup-block" style="display:none;">
|
||||||
const res = await fetch(`${API_URL}?action=check_security_status`);
|
<p>Système non initialisé.</p>
|
||||||
const data = await res.json();
|
<button onclick="doLoginBlank()">Se connecter sans mot de passe</button>
|
||||||
|
</div>
|
||||||
if (data.is_blank === true) {
|
</div>
|
||||||
formBlock.style.display = 'none';
|
|
||||||
blankBlock.style.display = 'block';
|
<script>
|
||||||
} else {
|
const API_URL = '../api.php';
|
||||||
formBlock.style.display = 'block';
|
|
||||||
blankBlock.style.display = 'none';
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
|
const formBlock = document.getElementById('login-form-block');
|
||||||
|
const blankBlock = document.getElementById('blank-setup-block');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_URL}?action=check_security_status`);
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
if (data.is_blank === true) {
|
||||||
|
formBlock.style.display = 'none';
|
||||||
|
blankBlock.style.display = 'block';
|
||||||
|
} else {
|
||||||
|
formBlock.style.display = 'block';
|
||||||
|
blankBlock.style.display = 'none';
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
console.error("Erreur API :", e);
|
||||||
|
alert("Erreur de connexion au serveur.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function doLoginBlank() { await performLogin(""); }
|
||||||
|
async function doLogin() {
|
||||||
|
const pwd = document.getElementById('login-pwd').value;
|
||||||
|
await performLogin(pwd);
|
||||||
}
|
}
|
||||||
} catch(e) {
|
|
||||||
console.error("Erreur API :", e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
async function doLoginBlank() {
|
async function performLogin(pwd) {
|
||||||
await performLogin(""); // Envoie un mot de passe vide
|
try {
|
||||||
}
|
const res = await fetch(`${API_URL}?action=login`, {
|
||||||
|
method: 'POST',
|
||||||
async function doLogin() {
|
headers: { 'Content-Type': 'application/json' },
|
||||||
const pwd = document.getElementById('login-pwd').value;
|
body: JSON.stringify({ password: pwd })
|
||||||
await performLogin(pwd);
|
});
|
||||||
}
|
const data = await res.json();
|
||||||
|
if (data.success) {
|
||||||
async function performLogin(pwd) {
|
localStorage.setItem('token', data.token);
|
||||||
try {
|
if (data.blank === true) {
|
||||||
const res = await fetch(`${API_URL}?action=login`, {
|
const newPwd = prompt("Définissez votre mot de passe (min 4 caractères) :");
|
||||||
method: 'POST',
|
if (newPwd && newPwd.length >= 4) {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
await fetch(`${API_URL}?action=setup_admin`, {
|
||||||
body: JSON.stringify({ password: pwd })
|
method: 'POST',
|
||||||
});
|
headers: { 'Content-Type': 'application/json', 'Authorization': data.token },
|
||||||
|
body: JSON.stringify({ password: newPwd })
|
||||||
const data = await res.json();
|
});
|
||||||
console.log("Réponse login :", data);
|
}
|
||||||
|
}
|
||||||
if (data.success) {
|
window.location.href = 'dashboard.html';
|
||||||
localStorage.setItem('token', data.token);
|
} else {
|
||||||
|
alert("Erreur : " + (data.error || "Mot de passe incorrect"));
|
||||||
// Si blank, on force la création du mot de passe
|
}
|
||||||
if (data.blank === true) {
|
} catch(e) {
|
||||||
const newPwd = prompt("Système initialisé. Définissez votre mot de passe (min 4 caractères) :");
|
alert("Erreur technique : " + e.message);
|
||||||
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';
|
</script>
|
||||||
} else {
|
</body>
|
||||||
alert("Erreur : " + (data.error || "Mot de passe incorrect"));
|
</html>
|
||||||
}
|
|
||||||
} catch(e) {
|
|
||||||
alert("Erreur technique : " + e.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
Reference in New Issue
Block a user