Compare commits
2 Commits
243f56a1ec
...
7b532af758
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b532af758 | ||
|
|
cd65291dcd |
@ -29,7 +29,7 @@ document.getElementById("registerForm").addEventListener("submit", async functio
|
|||||||
// Button loading state
|
// Button loading state
|
||||||
const submitBtn = this.querySelector("button[type='submit']");
|
const submitBtn = this.querySelector("button[type='submit']");
|
||||||
const originalText = submitBtn.textContent;
|
const originalText = submitBtn.textContent;
|
||||||
submitBtn.innerHTML = "<span>Loading...</span>";
|
|
||||||
submitBtn.disabled = true;
|
submitBtn.disabled = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -56,7 +56,6 @@ document.getElementById("registerForm").addEventListener("submit", async functio
|
|||||||
showModal("error", "Error!", msg);
|
showModal("error", "Error!", msg);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
submitBtn.textContent = originalText;
|
|
||||||
submitBtn.disabled = false;
|
submitBtn.disabled = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
62
Register.php
62
Register.php
@ -1,36 +1,84 @@
|
|||||||
<?php
|
<?php
|
||||||
header('Content-Type: application/json');
|
// ✅ CORS Headers - di paling atas
|
||||||
|
header('Access-Control-Allow-Origin: *');
|
||||||
|
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
|
||||||
|
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
||||||
|
header('Access-Control-Max-Age: 86400');
|
||||||
|
header('Content-Type: application/json'); // Cukup 1x saja
|
||||||
|
|
||||||
|
// ✅ Handle preflight OPTIONS
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||||
|
http_response_code(200);
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
include 'Connection.php';
|
include 'Connection.php';
|
||||||
|
|
||||||
$username = $_POST['username'] ?? '';
|
// ✅ Handle input dari JSON body atau POST form
|
||||||
$password = $_POST['password'] ?? '';
|
$input = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$username = trim($input['username'] ?? $_POST['username'] ?? '');
|
||||||
|
$password = $input['password'] ?? $_POST['password'] ?? '';
|
||||||
|
|
||||||
|
// ✅ Validasi input kosong
|
||||||
if (empty($username) || empty($password)) {
|
if (empty($username) || empty($password)) {
|
||||||
echo json_encode(["status" => "error", "message" => "Username dan password wajib diisi"]);
|
echo json_encode([
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Username dan password wajib diisi"
|
||||||
|
]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ✅ Validasi panjang password minimal
|
||||||
|
if (strlen($password) < 6) {
|
||||||
|
echo json_encode([
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Password minimal 6 karakter"
|
||||||
|
]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Validasi format username (opsional tapi disarankan)
|
||||||
|
if (!preg_match('/^[a-zA-Z0-9_]{3,20}$/', $username)) {
|
||||||
|
echo json_encode([
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Username hanya boleh huruf, angka, underscore (3-20 karakter)"
|
||||||
|
]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Cek apakah username sudah ada
|
||||||
$check = $conn->prepare("SELECT id FROM users WHERE username = ?");
|
$check = $conn->prepare("SELECT id FROM users WHERE username = ?");
|
||||||
$check->bind_param("s", $username);
|
$check->bind_param("s", $username);
|
||||||
$check->execute();
|
$check->execute();
|
||||||
$check->store_result();
|
$check->store_result();
|
||||||
|
|
||||||
if ($check->num_rows > 0) {
|
if ($check->num_rows > 0) {
|
||||||
echo json_encode(["status" => "error", "message" => "Username sudah digunakan"]);
|
echo json_encode([
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Username sudah digunakan"
|
||||||
|
]);
|
||||||
$check->close();
|
$check->close();
|
||||||
$conn->close();
|
$conn->close();
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
$check->close();
|
||||||
|
|
||||||
|
// ✅ Hash password dan insert ke database
|
||||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
||||||
$stmt->bind_param("ss", $username, $hashedPassword);
|
$stmt->bind_param("ss", $username, $hashedPassword);
|
||||||
|
|
||||||
if ($stmt->execute()) {
|
if ($stmt->execute()) {
|
||||||
echo json_encode(["status" => "success", "message" => "Pendaftaran berhasil"]);
|
echo json_encode([
|
||||||
|
"status" => "success",
|
||||||
|
"message" => "Pendaftaran berhasil"
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
echo json_encode(["status" => "error", "message" => "Gagal mendaftar"]);
|
echo json_encode([
|
||||||
|
"status" => "error",
|
||||||
|
"message" => "Gagal mendaftar: " . $conn->error
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
const BASE_URL = "http://202.46.28.160/Kelompok06_2048/";
|
const BASE_URL = "http://localhost/Kelompok06_2048/";
|
||||||
|
|
||||||
export async function registerRequest(username, password) {
|
export async function registerRequest(username, password) {
|
||||||
if (!username || !password) {
|
if (!username || !password) {
|
||||||
@ -10,7 +10,7 @@ export async function registerRequest(username, password) {
|
|||||||
formData.append("password", password);
|
formData.append("password", password);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${BASE_URL}Register.php`, {
|
const response = await fetch(`${BASE_URL}/Register.php`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Accept": "application/json"
|
"Accept": "application/json"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user