fixed login
login now throws errors when wrong credentials are used and properly redirects to dashboard
This commit is contained in:
34
js/login.js
34
js/login.js
@@ -9,13 +9,37 @@ document.getElementById("loginForm").addEventListener("submit", async function (
|
|||||||
|
|
||||||
const response = await fetch("../php/login.php", {
|
const response = await fetch("../php/login.php", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: formData
|
body: formData,
|
||||||
|
redirect: "follow"
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await response.json();
|
// If the server redirected (PHP currently redirects on success/failure), handle accordingly
|
||||||
if (result.success) {
|
if (response.redirected) {
|
||||||
window.location.href = "../php/admin.php";
|
// if redirected back to login with error flag, show message instead of navigating
|
||||||
|
if (response.url && response.url.includes("error=1")) {
|
||||||
|
document.getElementById("errorMsg").innerText = "Login fehlgeschlagen!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
window.location.href = response.url;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentType = response.headers.get("content-type") || "";
|
||||||
|
if (contentType.includes("application/json")) {
|
||||||
|
const result = await response.json();
|
||||||
|
if (result.success) {
|
||||||
|
window.location.href = "../php/admin.php";
|
||||||
|
} else {
|
||||||
|
document.getElementById("errorMsg").innerText = "Login fehlgeschlagen!";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
document.getElementById("errorMsg").innerText = "Login fehlgeschlagen!";
|
const text = await response.text();
|
||||||
|
if (text.includes("Benutzerdaten fehlen") || text.includes("error=1")) {
|
||||||
|
document.getElementById("errorMsg").innerText = "Login fehlgeschlagen!";
|
||||||
|
} else if (response.url && response.url.includes("admin.php")) {
|
||||||
|
window.location.href = response.url;
|
||||||
|
} else {
|
||||||
|
document.getElementById("errorMsg").innerText = "Server-Antwort unklar.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,14 +12,14 @@
|
|||||||
<h2>Admin Login</h2>
|
<h2>Admin Login</h2>
|
||||||
<img src="../img/csg.png" width="100" />
|
<img src="../img/csg.png" width="100" />
|
||||||
</div>
|
</div>
|
||||||
<form id="loginForm" action="../php/login.php" method="post">
|
<form id="loginForm">
|
||||||
<label for="username">Benutzername</label>
|
|
||||||
<input type="text" id="username" name="username" required />
|
<input type="text" id="username" name="username" required />
|
||||||
<label for="password">Passwort</label>
|
<label for="password">Passwort</label>
|
||||||
<input type="password" id="password" name="password" required />
|
<input type="password" id="password" name="password" required />
|
||||||
<button type="submit">Einloggen</button>
|
<button type="submit">Einloggen</button>
|
||||||
</form>
|
</form>
|
||||||
|
<p id="errorMsg" style="color: red;"></p>
|
||||||
</div>
|
</div>
|
||||||
<!--<script src="../js/login.js"></script>-->
|
<script src="../js/login.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user