diff --git a/auth.php b/auth.php index 467505c..1b8d5d1 100644 --- a/auth.php +++ b/auth.php @@ -1,74 +1,63 @@ alert('Semua data wajib diisi!'); window.location='index.php';"; + // validasi + if (!$username || !$email || !$password || !$confirm) { + header("Location: index.php?error=kosong"); exit; } - // Cek Format Email (Biar gak ngawur) if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { - echo ""; + header("Location: index.php?error=email"); exit; } - // Cek Panjang Password (Minimal 6) if (strlen($password) < 6) { - echo ""; + header("Location: index.php?error=pass"); exit; } - // Cek Kesamaan Password if ($password !== $confirm) { - echo ""; + header("Location: index.php?error=confirm"); exit; } - // --- 2. CEK DUPLIKAT DI DATABASE --- - $stmt = mysqli_prepare($conn, "SELECT id FROM users WHERE username = ? OR email = ?"); - mysqli_stmt_bind_param($stmt, "ss", $username, $email); - mysqli_stmt_execute($stmt); - mysqli_stmt_store_result($stmt); + // cek user + $cek = mysqli_prepare($conn, "SELECT id FROM users WHERE username=? OR email=?"); + mysqli_stmt_bind_param($cek, "ss", $username, $email); + mysqli_stmt_execute($cek); + mysqli_stmt_store_result($cek); - if (mysqli_stmt_num_rows($stmt) > 0) { - echo ""; + if (mysqli_stmt_num_rows($cek) > 0) { + header("Location: index.php?error=exist"); exit; } - mysqli_stmt_close($stmt); - // --- 3. SIMPAN DATA --- - $hashed_password = password_hash($password, PASSWORD_DEFAULT); - $stmtInsert = mysqli_prepare($conn, "INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); - mysqli_stmt_bind_param($stmtInsert, "sss", $username, $email, $hashed_password); + mysqli_stmt_close($cek); - if (mysqli_stmt_execute($stmtInsert)) { - echo ""; - } else { - echo "Error: " . mysqli_error($conn); - } - mysqli_stmt_close($stmtInsert); + // simpan + $hash = password_hash($password, PASSWORD_DEFAULT); + $insert = mysqli_prepare($conn, "INSERT INTO users (username,email,password) VALUES (?,?,?)"); + mysqli_stmt_bind_param($insert, "sss", $username, $email, $hash); + mysqli_stmt_execute($insert); + mysqli_stmt_close($insert); -} + header("Location: index.php?success=register"); + exit; +} + +// ===================== LOGIN ===================== +if (isset($_POST['btn-login'])) { -// ========================================== -// BAGIAN 2: LOGIKA LOGIN -// ========================================== -else if (isset($_POST['btn-login'])) { - $username = $_POST['username']; $password = $_POST['password']; @@ -77,20 +66,15 @@ else if (isset($_POST['btn-login'])) { mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); - $row = mysqli_fetch_assoc($result); + $user = mysqli_fetch_assoc($result); - // JIKA GAGAL - if (!$row || !password_verify($password, $row['password'])) { - // Kirim sinyal error ke HTML (Kotak Merah) - header("Location: index.php?error=gagal"); + if (!$user || !password_verify($password, $user['password'])) { + header("Location: index.php?error=login"); exit; } - // JIKA SUKSES - $_SESSION['username'] = $row['username']; - $_SESSION['login'] = true; + $_SESSION['user'] = $user; - header("Location: mainboard.html"); + header("Location: mainboard.php"); exit; } -?> \ No newline at end of file