From 717669245d53b607db6527f443bf7ef410fbe7d3 Mon Sep 17 00:00:00 2001 From: Stanley Date: Tue, 16 Dec 2025 09:52:18 +0700 Subject: [PATCH] Updsate --- Login1.html | 145 ++++++++++++++++++++++++++++-------- api/config.php | 34 +++++++++ api/login.php | 33 ++++++++ api/logout.php | 12 +++ api/me.php | 6 ++ api/register.php | 41 ++++++++++ img/module_table_bottom.png | Bin 0 -> 751 bytes img/module_table_top.png | Bin 0 -> 337 bytes 8 files changed, 241 insertions(+), 30 deletions(-) create mode 100644 api/config.php create mode 100644 api/login.php create mode 100644 api/logout.php create mode 100644 api/me.php create mode 100644 api/register.php create mode 100644 img/module_table_bottom.png create mode 100644 img/module_table_top.png diff --git a/Login1.html b/Login1.html index e2e1c9c..b587d5e 100644 --- a/Login1.html +++ b/Login1.html @@ -1,11 +1,13 @@ + Space Odyssey +
@@ -14,66 +16,149 @@

SPACE
ODYSSEY

-
Initializing Spaceship
+
Initializing Spaceship
-

Access Terminal

- - - +

Access Terminal

+ + + + - +
- +
+
+
+ \ No newline at end of file diff --git a/api/config.php b/api/config.php new file mode 100644 index 0000000..f23828c --- /dev/null +++ b/api/config.php @@ -0,0 +1,34 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]); +} catch (Throwable $e) { + json_out(500, ['ok' => false, 'error' => 'DB connection failed']); +} + +function current_user(PDO $pdo): ?array { + if (!isset($_SESSION['user_id'])) return null; + $stmt = $pdo->prepare('SELECT id, username, email, created_at FROM users WHERE id = ? LIMIT 1'); + $stmt->execute([(int)$_SESSION['user_id']]); + $u = $stmt->fetch(); + return $u ?: null; +} diff --git a/api/login.php b/api/login.php new file mode 100644 index 0000000..f60d499 --- /dev/null +++ b/api/login.php @@ -0,0 +1,33 @@ + false, 'error' => 'Invalid JSON']); + +$login = isset($data['login']) ? trim((string)$data['login']) : ''; +$password = isset($data['password']) ? (string)$data['password'] : ''; + +if ($login === '' || $password === '') { + json_out(400, ['ok' => false, 'error' => 'Missing login or password']); +} + +$stmt = $pdo->prepare('SELECT id, username, email, password_hash, created_at FROM users WHERE username = ? OR email = ? LIMIT 1'); +$stmt->execute([$login, $login]); +$user = $stmt->fetch(); + +if (!$user || !password_verify($password, (string)$user['password_hash'])) { + json_out(401, ['ok' => false, 'error' => 'Invalid credentials']); +} + +$_SESSION['user_id'] = (int)$user['id']; + +json_out(200, [ + 'ok' => true, + 'user' => [ + 'id' => (int)$user['id'], + 'username' => (string)$user['username'], + 'email' => $user['email'], + 'created_at' => $user['created_at'], + ] +]); diff --git a/api/logout.php b/api/logout.php new file mode 100644 index 0000000..aa0f90b --- /dev/null +++ b/api/logout.php @@ -0,0 +1,12 @@ + true]); diff --git a/api/me.php b/api/me.php new file mode 100644 index 0000000..5d7388a --- /dev/null +++ b/api/me.php @@ -0,0 +1,6 @@ + true, 'user' => $u]); diff --git a/api/register.php b/api/register.php new file mode 100644 index 0000000..efa90fe --- /dev/null +++ b/api/register.php @@ -0,0 +1,41 @@ + false, 'error' => 'Invalid JSON']); + +$username = isset($data['username']) ? trim((string)$data['username']) : ''; +$email = isset($data['email']) ? trim((string)$data['email']) : ''; +$password = isset($data['password']) ? (string)$data['password'] : ''; + +if ($username === '' || strlen($username) < 3 || strlen($username) > 32) { + json_out(400, ['ok' => false, 'error' => 'Username must be 3-32 chars']); +} +if (!preg_match('/^[A-Za-z0-9_]+$/', $username)) { + json_out(400, ['ok' => false, 'error' => 'Username must be letters/numbers/_ only']); +} +if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) { + json_out(400, ['ok' => false, 'error' => 'Invalid email']); +} +if (strlen($password) < 6) { + json_out(400, ['ok' => false, 'error' => 'Password must be at least 6 chars']); +} + +$hash = password_hash($password, PASSWORD_DEFAULT); + +try { + $stmt = $pdo->prepare('INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)'); + $stmt->execute([$username, ($email === '' ? null : $email), $hash]); + + $_SESSION['user_id'] = (int)$pdo->lastInsertId(); + $u = current_user($pdo); + + json_out(201, ['ok' => true, 'user' => $u]); +} catch (Throwable $e) { + $msg = $e->getMessage(); + if (stripos($msg, 'Duplicate') !== false || stripos($msg, 'uq_') !== false) { + json_out(409, ['ok' => false, 'error' => 'Username or email already used']); + } + json_out(500, ['ok' => false, 'error' => 'Register failed']); +} diff --git a/img/module_table_bottom.png b/img/module_table_bottom.png new file mode 100644 index 0000000000000000000000000000000000000000..536f75736f058d23e9bc807bc12e95cf61463949 GIT binary patch literal 751 zcmVPx#x=>71MgRZ*_xJbm^78cb^!)t%`T6gw&-*xcaYDSlX+1cIV;^pt} z@YdGa^78ZY^7Q)p`{(EB;Naux>g?s^=jG+--rnKx@bU2Q^7;At>FMk2?C$>l|L5oG z`}_REy%%r*00GlUL_t(&-tC#kcET_eL|r30k}=qH2sH_*klyqEzZF$VvdG$ga~J3w zmUHLHvg{2K4^ZWM;te@N4n+AZr(W^;h9HQ-KEAwi$`J%nHq+Acj#30cl#_S$GeQtV zxBttHT29-Df+X!Ty>u@kWI>SVqN`VaULXvD6f1M`=e)oZfZ*<5aC7ocdD*)~9>loq zbyMp<<<~cBBtnYy%{AZokK4U5G9kpcH|9Jw=l)0)pmR0CJrEVr(N#z3xBjD)^H`h@ zmiQD%fn+&Y#j)J|cmCxDAVgVA9_EOr=FCqfkD1kP<~^|m@EgJDWq;9`A*7lz>n!@i zr*7=@TmP;AvH{u4+4JxS)RX@GT`81(tKa$GFF+QCdC+RLn$6}hs5DYxQ<@BdJPfm} z(eQ;3O1`vj^fTw*7C<*ZS0K#uJOC9(N|fr2d*2sk^p$<9-}q__6~HRRc{3 zs))6hDU;g0+xNA6Wnb!d{jE(IcjQU{YDKi~{?Puf2G|mS`j8yJ*l+u;0Q(&P^@9E3&+QNc1o-EQ h5CM_Y4gdgv7(aPbjz*g0Zb1M5002ovPDHLkV1ksLwQT?Z literal 0 HcmV?d00001 diff --git a/img/module_table_top.png b/img/module_table_top.png new file mode 100644 index 0000000000000000000000000000000000000000..5174a22c4b52a3fdb14b90845928bdf64dbadd65 GIT binary patch literal 337 zcmV-X0j~auP)Px#Y*0*8MgRZ*{QUga*WB#v?&jv{>FMkG`uo(>+4%VR z)z#VZ^7HEI?ECxs^YirR=<4I+=HlY!+1cIp_V?Z0;r{;r{r&yc*4ypv@BaS&*4EnU z>h0Ux-|Oq{+}z;z_xSPg^6>ET=;-U|>Fn$4?KS^8MF0Q*gGod|RA}Dq)majPFaQM6 z07*z>UlkNky#F2f=L*yB3O!Y0a;S(y`|;)`HxM!QN8N2_Nq=E`k=6