Register
This commit is contained in:
parent
4aca5133ea
commit
c2922d0f1d
27
Register.js
27
Register.js
@ -28,17 +28,34 @@ 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;
|
|
||||||
|
|
||||||
submitBtn.disabled = true;
|
submitBtn.disabled = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await registerRequest(username, password);
|
const data = await registerRequest(username, password);
|
||||||
|
|
||||||
// API temenmu return: { status: "success", message: "..." }
|
|
||||||
if (data.status === "success") {
|
if (data.status === "success") {
|
||||||
|
// 1. Tampilkan modal sukses
|
||||||
showModal("success", "Register Successful!", data.message);
|
showModal("success", "Register Successful!", data.message);
|
||||||
|
|
||||||
|
// 🔥 PERBAIKAN: SAVE SCORE & REDIRECT 🔥
|
||||||
|
|
||||||
|
// Coba simpan skor jika variable 'score' atau 'gameScore' tersedia di global scope
|
||||||
|
// Atau jika kamu menyimpan skor sementara di localStorage
|
||||||
|
const pendingScore = localStorage.getItem('lastScore'); // Contoh jika pakai localStorage
|
||||||
|
|
||||||
|
if (pendingScore && typeof saveScore === "function") {
|
||||||
|
console.log("Menyimpan skor pending: " + pendingScore);
|
||||||
|
saveScore(pendingScore);
|
||||||
|
} else if (typeof score !== 'undefined') {
|
||||||
|
// Jika variabel global 'score' ada (dari file game logic)
|
||||||
|
saveScore(score);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect ke halaman utama setelah 1.5 detik
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = "index.html"; // Ubah sesuai halaman game/menu kamu
|
||||||
|
}, 1500);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
showModal("error", "Register Failed!", data.message || "An error occurred.");
|
showModal("error", "Register Failed!", data.message || "An error occurred.");
|
||||||
setInputError("username");
|
setInputError("username");
|
||||||
@ -48,11 +65,9 @@ document.getElementById("registerForm").addEventListener("submit", async functio
|
|||||||
console.error("Register Error:", error);
|
console.error("Register Error:", error);
|
||||||
|
|
||||||
let msg = "A connection error occurred.";
|
let msg = "A connection error occurred.";
|
||||||
|
|
||||||
if (error.message === "Failed to fetch") {
|
if (error.message === "Failed to fetch") {
|
||||||
msg = "Unable to connect to server.";
|
msg = "Unable to connect to server.";
|
||||||
}
|
}
|
||||||
|
|
||||||
showModal("error", "Error!", msg);
|
showModal("error", "Error!", msg);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
@ -68,9 +83,11 @@ document.getElementById("confirmPassword").addEventListener("input", clearInputS
|
|||||||
// Efek error merah neon
|
// Efek error merah neon
|
||||||
function setInputError(id) {
|
function setInputError(id) {
|
||||||
const el = document.getElementById(id);
|
const el = document.getElementById(id);
|
||||||
|
if(el) {
|
||||||
el.style.borderColor = "#ff0080";
|
el.style.borderColor = "#ff0080";
|
||||||
el.style.boxShadow = "0 0 20px rgba(255, 0, 128, 0.3)";
|
el.style.boxShadow = "0 0 20px rgba(255, 0, 128, 0.3)";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Hilangkan error visual
|
// Hilangkan error visual
|
||||||
function clearInputStyle() {
|
function clearInputStyle() {
|
||||||
|
|||||||
28
Register.php
28
Register.php
@ -2,12 +2,12 @@
|
|||||||
// ✅ Set timezone Indonesia (WIB)
|
// ✅ Set timezone Indonesia (WIB)
|
||||||
date_default_timezone_set('Asia/Jakarta');
|
date_default_timezone_set('Asia/Jakarta');
|
||||||
|
|
||||||
// ✅ CORS Headers - di paling atas
|
// ✅ CORS Headers
|
||||||
header('Access-Control-Allow-Origin: *');
|
header('Access-Control-Allow-Origin: *');
|
||||||
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
|
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
|
||||||
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
||||||
header('Access-Control-Max-Age: 86400');
|
header('Access-Control-Max-Age: 86400');
|
||||||
header('Content-Type: application/json'); // Cukup 1x saja
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
// ✅ Handle preflight OPTIONS
|
// ✅ Handle preflight OPTIONS
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||||
@ -17,7 +17,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|||||||
|
|
||||||
include 'Connection.php';
|
include 'Connection.php';
|
||||||
|
|
||||||
// ✅ Handle input dari JSON body atau POST form
|
// ✅ Handle input
|
||||||
$input = json_decode(file_get_contents('php://input'), true);
|
$input = json_decode(file_get_contents('php://input'), true);
|
||||||
$username = trim($input['username'] ?? $_POST['username'] ?? '');
|
$username = trim($input['username'] ?? $_POST['username'] ?? '');
|
||||||
$password = $input['password'] ?? $_POST['password'] ?? '';
|
$password = $input['password'] ?? $_POST['password'] ?? '';
|
||||||
@ -31,7 +31,7 @@ if (empty($username) || empty($password)) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Validasi panjang password minimal
|
// ✅ Validasi panjang password
|
||||||
if (strlen($password) < 6) {
|
if (strlen($password) < 6) {
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"status" => "error",
|
"status" => "error",
|
||||||
@ -67,26 +67,24 @@ if ($check->num_rows > 0) {
|
|||||||
$check->close();
|
$check->close();
|
||||||
|
|
||||||
// ✅ Hash password dan insert ke database
|
// ✅ Hash password dan insert ke database
|
||||||
|
|
||||||
// ✅ Hash password (TETAP)
|
|
||||||
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
// 👉 1. TAMBAH INI: Simpan waktu WIB dari PHP ke variabel
|
|
||||||
$created_at = date("Y-m-d H:i:s");
|
$created_at = date("Y-m-d H:i:s");
|
||||||
|
|
||||||
// 👉 2. UBAH INI: Tambahkan kolom waktu ke dalam query
|
|
||||||
// (Pastikan nama kolom di database kamu 'created_at'. Kalau beda, sesuaikan namanya!)
|
|
||||||
$stmt = $conn->prepare("INSERT INTO users (username, password, created_at) VALUES (?, ?, ?)");
|
$stmt = $conn->prepare("INSERT INTO users (username, password, created_at) VALUES (?, ?, ?)");
|
||||||
|
|
||||||
// 👉 3. UBAH INI: Ubah "ss" jadi "sss" dan masukkan variabel $created_at
|
|
||||||
$stmt->bind_param("sss", $username, $hashedPassword, $created_at);
|
$stmt->bind_param("sss", $username, $hashedPassword, $created_at);
|
||||||
|
|
||||||
// ✅ Eksekusi (TETAP)
|
|
||||||
if ($stmt->execute()) {
|
if ($stmt->execute()) {
|
||||||
|
// 🔥 PERBAIKAN UTAMA DI SINI (AUTO-LOGIN) 🔥
|
||||||
|
$new_user_id = $stmt->insert_id; // Ambil ID user baru
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
$_SESSION['user_id'] = $new_user_id; // Set Session ID
|
||||||
|
$_SESSION['username'] = $username; // Set Session Username
|
||||||
|
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
"status" => "success",
|
"status" => "success",
|
||||||
"message" => "Pendaftaran berhasil",
|
"message" => "Pendaftaran berhasil & Auto-login",
|
||||||
"registered_at" => $created_at // Mengirimkan waktu yang sama ke JSON response
|
"registered_at" => $created_at
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user