From 6a10ff81f6b315cc9996ed4688e03469058539c2 Mon Sep 17 00:00:00 2001 From: trinkey Date: Sat, 22 Mar 2025 08:32:13 -0400 Subject: [PATCH] logging in works now --- index.php | 3 +++ login.php | 49 ++++++++++++++++++++++++++++++++++--------------- signup.php | 11 +++-------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/index.php b/index.php index 039f5fe..134362e 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,9 @@ Log In diff --git a/login.php b/login.php index 4182595..fd3ccaa 100644 --- a/login.php +++ b/login.php @@ -1,28 +1,47 @@ $u) - ); + if (!($u && $p)) { + $err = "Bad request, missing username or password parameter"; + } else if (strlen($u) > 64 || strlen($u) === 0) { + $err = "Username must be 1-64 chars"; + } else if (!preg_match("/^[a-z0-9_-]{1,64}$/", $u)) { + $err = "Username can only include a-z, 0-9, _, and -"; + } else { + $query = "SELECT password_hash FROM users WHERE username='$u' LIMIT 1;"; - if (sizeof($response) === 0) { + $response = pg_query($db, $query); + + if (pg_num_rows($response) === 0) { $err = "User '" . htmlspecialchars($u) . "' not found"; } else { - echo json_encode($response); + $user = pg_fetch_array($response); + if (password_verify($p, $user["password_hash"])) { + setcookie( + "token", + $token, + time() + 60 * 60 * 24 * 30 * 265 // 1 year from now + ); + header("Location: index.php"); + exit(); + } else { + $err = "Incorrect password"; + } } - } else { - $err = "Bad request, missing username or password parameter"; } - } else { - $u = ""; - $p = ""; } $title = "Log In"; diff --git a/signup.php b/signup.php index 50745db..1e0b0a4 100644 --- a/signup.php +++ b/signup.php @@ -78,7 +78,7 @@ } } - $title = "Log In"; + $title = "Sign Up"; include "boilerplate/head.php"; ?> @@ -86,15 +86,10 @@
-
+
+

Log in instead?

Sign up instead?

"; - } - include "boilerplate/foot.php"; ?>