This commit is contained in:
Nathan 2025-12-09 15:58:38 +07:00
parent 3fb9ae5e9e
commit 99c33ab04f
4 changed files with 153 additions and 143 deletions

View File

@ -313,9 +313,9 @@ input:focus {
</p> </p>
<script> <script>
/*LOGIN VALIDATION*/ /* LOGIN VALIDATION FRONT-END RINGAN */
document.getElementById("loginForm").addEventListener("submit", function(e) { document.getElementById("loginForm").addEventListener("submit", function(e) {
e.preventDefault(); e.preventDefault(); // jangan reload halaman
const username = document.getElementById("username").value.trim(); const username = document.getElementById("username").value.trim();
const password = document.getElementById("password").value.trim(); const password = document.getElementById("password").value.trim();
@ -324,6 +324,7 @@ document.getElementById("loginForm").addEventListener("submit", function(e) {
errorBox.style.display = "none"; errorBox.style.display = "none";
errorBox.innerText = ""; errorBox.innerText = "";
// Validasi form
if (!username || !password) { if (!username || !password) {
showError("Username dan password harus diisi"); showError("Username dan password harus diisi");
return; return;
@ -334,24 +335,27 @@ document.getElementById("loginForm").addEventListener("submit", function(e) {
return; return;
} }
const usersData = localStorage.getItem("users"); // Kirim ke login.php via AJAX
const users = usersData ? JSON.parse(usersData) : []; fetch("login.php", {
method: "POST",
const user = users.find(u => u.username === username); headers: {"Content-Type": "application/x-www-form-urlencoded"},
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
if (!user) { })
showError("Username tidak ditemukan"); .then(response => response.text())
return; .then(data => {
data = data.trim();
if (data === "OK") {
// login sukses -> redirect ke mainboard.php
window.location.href = "mainboard.php";
} else {
// tampilkan error dari PHP
showError(data);
} }
})
if (user.password !== password) { .catch(err => {
showError("Password salah"); showError("Terjadi kesalahan server");
return; console.error(err);
} });
// Login sukses
localStorage.setItem("loggedInUser", JSON.stringify(user));
window.location.href = "mainboard.html";
}); });
function showError(msg) { function showError(msg) {
@ -374,15 +378,11 @@ function toggleDemo() {
} }
} }
/* GLITTER EFFECT */ /* GLITTER EFFECT */
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
const card = document.querySelector(".login-card"); const card = document.querySelector(".login-card");
if (!card) return; if (!card) return;
// Tambah glitter 20x
for (let i = 0; i < 20; i++) { for (let i = 0; i < 20; i++) {
const g = document.createElement("div"); const g = document.createElement("div");
g.className = "glitter"; g.className = "glitter";
@ -408,7 +408,7 @@ setInterval(() => {
g.style.top = Math.random() * window.innerHeight + "px"; g.style.top = Math.random() * window.innerHeight + "px";
}); });
}, 900); }, 900);
</script> </script>
</body> </body>
</html> </html>

View File

@ -5,22 +5,17 @@ include "koneksi.php";
$username = $_POST['username']; $username = $_POST['username'];
$password = $_POST['password']; $password = $_POST['password'];
$enc = md5($password); $stmt = mysqli_prepare($conn, "SELECT * FROM user WHERE username=?");
mysqli_stmt_bind_param($stmt, "s", $username);
$stmt = mysqli_prepare($conn, "SELECT * FROM user WHERE username=? AND password=?");
mysqli_stmt_bind_param($stmt, "ss", $username, $enc);
mysqli_stmt_execute($stmt); mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt); $result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
if ($row && password_verify($password, $row['password'])) {
$_SESSION['username'] = $row['username']; $_SESSION['username'] = $row['username'];
$_SESSION['email'] = $row['email']; echo "OK";
header("Location: mainboard.html");
exit; exit;
} else { } else {
echo "<script>alert('Username atau password salah!'); window.history.back();</script>"; echo "Username atau password salah";
exit;
} }
?> ?>

View File

@ -256,17 +256,18 @@ input:focus {
<script> <script>
document.getElementById("registerForm").addEventListener("submit", function(e) { document.getElementById("registerForm").addEventListener("submit", function(e) {
e.preventDefault(); e.preventDefault(); // jangan reload halaman
let username = document.getElementById("username").value.trim(); const username = document.getElementById("username").value.trim();
let email = document.getElementById("email").value.trim(); const email = document.getElementById("email").value.trim();
let password = document.getElementById("password").value.trim(); const password = document.getElementById("password").value.trim();
let confirmPassword = document.getElementById("confirm_password").value.trim(); const confirmPassword = document.getElementById("confirm_password").value.trim();
let errorBox = document.getElementById("errorBox"); const errorBox = document.getElementById("errorBox");
errorBox.style.display = "none"; errorBox.style.display = "none";
errorBox.innerText = "";
// Validasi // Validasi form
if (!username || !email || !password || !confirmPassword) { if (!username || !email || !password || !confirmPassword) {
showError("Semua field harus diisi"); showError("Semua field harus diisi");
return; return;
@ -282,44 +283,39 @@ input:focus {
return; return;
} }
if (password !== confirm_password) { if (password !== confirmPassword) {
showError("Password dan konfirmasi password tidak cocok"); showError("Password dan konfirmasi password tidak cocok");
return; return;
} }
let users = JSON.parse(localStorage.getItem("users") || "[]"); // Kirim ke register.php via AJAX
fetch("register.php", {
if (users.some(u => u.username === username)) { method: "POST",
showError("Username sudah digunakan"); headers: {"Content-Type": "application/x-www-form-urlencoded"},
return; body: `username=${encodeURIComponent(username)}&email=${encodeURIComponent(email)}&password=${encodeURIComponent(password)}`
})
.then(response => response.text())
.then(data => {
data = data.trim();
if (data === "OK") {
// registrasi sukses -> redirect ke mainboard.php
window.location.href = "mainboard.php";
} else {
showError(data); // tampilkan error dari PHP
} }
})
if (users.some(u => u.email === email)) { .catch(err => {
showError("Email sudah digunakan"); showError("Terjadi kesalahan server");
return; console.error(err);
}
// Simpan user baru
users.push({
id: Date.now().toString(),
username,
email,
password,
role: "player"
}); });
localStorage.setItem("users", JSON.stringify(users));
alert("Registrasi berhasil! Silakan login.");
window.location.href = "login.html";
}); });
function showError(msg) { function showError(msg) {
let box = document.getElementById("errorBox"); const box = document.getElementById("errorBox");
box.innerText = msg; box.innerText = msg;
box.style.display = "block"; box.style.display = "block";
} }
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,34 +1,53 @@
<?php <?php
session_start();
include "koneksi.php"; include "koneksi.php";
$username = $_POST['username']; if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email']; $username = trim($_POST['username']);
$password = $_POST['password']; $email = trim($_POST['email']);
$confirm = $_POST['confirm_password']; $password = trim($_POST['password']);
$confirmPassword = trim($_POST['confirm_password']);
if (!$username || !$email || !$password || !$confirmPassword) {
echo "<script>alert('Semua field harus diisi'); window.history.back();</script>";
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('Format email tidak valid!'); window.history.back();</script>"; echo "<script>alert('Format email tidak valid'); window.history.back();</script>";
exit; exit;
} }
if ($password != $confirm) { if ($password !== $confirmPassword) {
echo "<script>alert('Password tidak sama'); window.history.back();</script>"; echo "<script>alert('Password dan konfirmasi password tidak cocok'); window.history.back();</script>";
exit; exit;
} }
$cek = mysqli_query($conn, "SELECT * FROM user WHERE username='$username' OR email='$email'"); if (strlen($password) < 6) {
if (mysqli_num_rows($cek) > 0) { echo "<script>alert('Password minimal 6 karakter'); window.history.back();</script>";
exit;
}
// cek username & email di database
$stmt = mysqli_prepare($conn, "SELECT id FROM user WHERE username=? OR email=?");
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
if (mysqli_stmt_num_rows($stmt) > 0) {
echo "<script>alert('Username atau email sudah digunakan'); window.history.back();</script>"; echo "<script>alert('Username atau email sudah digunakan'); window.history.back();</script>";
exit; exit;
} }
$hash = md5($password); // hash password
$hash = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO user (username, email, password) VALUES ('$username', '$email', '$hash')"; // insert user
if (mysqli_query($conn, $sql)) { $stmt = mysqli_prepare($conn, "INSERT INTO user (username, email, password, role) VALUES (?, ?, ?, 'player')");
echo "<script>alert('Registrasi berhasil! Silakan login.'); window.location='login.html';</script>"; mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hash);
} else { mysqli_stmt_execute($stmt);
echo "Error: " . mysqli_error($conn);
echo "<script>alert('Registrasi berhasil! Silakan login.'); window.location.href='login.html';</script>";
exit;
} }
?> ?>