Register
This commit is contained in:
parent
c548614a94
commit
cd65291dcd
@ -29,7 +29,6 @@ document.getElementById("registerForm").addEventListener("submit", async functio
|
||||
// Button loading state
|
||||
const submitBtn = this.querySelector("button[type='submit']");
|
||||
const originalText = submitBtn.textContent;
|
||||
submitBtn.innerHTML = "<span>Memproses...</span>";
|
||||
submitBtn.disabled = true;
|
||||
|
||||
try {
|
||||
@ -56,7 +55,6 @@ document.getElementById("registerForm").addEventListener("submit", async functio
|
||||
showModal("error", "Error!", msg);
|
||||
|
||||
} finally {
|
||||
submitBtn.textContent = originalText;
|
||||
submitBtn.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
62
Register.php
62
Register.php
@ -1,36 +1,84 @@
|
||||
<?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';
|
||||
|
||||
$username = $_POST['username'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
// ✅ Handle input dari JSON body atau POST form
|
||||
$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)) {
|
||||
echo json_encode(["status" => "error", "message" => "Username dan password wajib diisi"]);
|
||||
echo json_encode([
|
||||
"status" => "error",
|
||||
"message" => "Username dan password wajib diisi"
|
||||
]);
|
||||
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->bind_param("s", $username);
|
||||
$check->execute();
|
||||
$check->store_result();
|
||||
|
||||
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();
|
||||
$conn->close();
|
||||
exit;
|
||||
}
|
||||
$check->close();
|
||||
|
||||
// ✅ Hash password dan insert ke database
|
||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
||||
$stmt->bind_param("ss", $username, $hashedPassword);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
echo json_encode(["status" => "success", "message" => "Pendaftaran berhasil"]);
|
||||
echo json_encode([
|
||||
"status" => "success",
|
||||
"message" => "Pendaftaran berhasil"
|
||||
]);
|
||||
} else {
|
||||
echo json_encode(["status" => "error", "message" => "Gagal mendaftar"]);
|
||||
echo json_encode([
|
||||
"status" => "error",
|
||||
"message" => "Gagal mendaftar: " . $conn->error
|
||||
]);
|
||||
}
|
||||
|
||||
$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) {
|
||||
if (!username || !password) {
|
||||
@ -10,7 +10,7 @@ export async function registerRequest(username, password) {
|
||||
formData.append("password", password);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${BASE_URL}Register.php`, {
|
||||
const response = await fetch(`${BASE_URL}/Register.php`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Accept": "application/json"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user