From 3b6c6e5774dec41a16da03d1bb8497b448cfa564 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 20 Apr 2025 10:46:32 +0500 Subject: feat: users, account management, authentication system --- public/account/delete.php | 39 +++++++++++ public/account/index.php | 59 ++++++++++++++++ public/account/login/index.php | 38 +++++++++++ public/account/login/twitch.php | 146 ++++++++++++++++++++++++++++++++++++++++ public/account/signout.php | 25 +++++++ 5 files changed, 307 insertions(+) create mode 100644 public/account/delete.php create mode 100644 public/account/index.php create mode 100644 public/account/login/index.php create mode 100644 public/account/login/twitch.php create mode 100644 public/account/signout.php (limited to 'public/account') diff --git a/public/account/delete.php b/public/account/delete.php new file mode 100644 index 0000000..af8a093 --- /dev/null +++ b/public/account/delete.php @@ -0,0 +1,39 @@ +prepare("UPDATE emotes SET uploaded_by = NULL WHERE uploaded_by = :id"); +$stmt->bindValue(":id", $id); +$stmt->execute(); + +$stmt = $db->prepare("DELETE FROM connections WHERE user_id = :id"); +$stmt->bindValue(":id", $id); +$stmt->execute(); + +$stmt = $db->prepare("DELETE FROM users WHERE id = :id"); +$stmt->bindValue(":id", $id); +$stmt->execute(); + +session_unset(); +session_destroy(); + +setcookie("secret_key", "", time() - 1000); + +$db->close(); + +$path = "../static/userdata/avatars/$id"; +if (is_file($path)) { + unlink($path); +} + +header("Location: /account"); \ No newline at end of file diff --git a/public/account/index.php b/public/account/index.php new file mode 100644 index 0000000..8f40ec9 --- /dev/null +++ b/public/account/index.php @@ -0,0 +1,59 @@ + + + + + + Account management - alright.party + + + + +
+
+ + +
+
+

Account management

+ +
+

Profile

+

Profile picture

+ " id="pfp" width="64" + height="64"> + + +

Username

+ "> + + +
+ +
+ +
+

Security

+ +
+ +
+ +
+
+
+
+
+ + + \ No newline at end of file diff --git a/public/account/login/index.php b/public/account/login/index.php new file mode 100644 index 0000000..146fde9 --- /dev/null +++ b/public/account/login/index.php @@ -0,0 +1,38 @@ + + + + + + Log in to alright.party + + + + +
+
+ + +
+
+ +
+
+ +
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/public/account/login/twitch.php b/public/account/login/twitch.php new file mode 100644 index 0000000..ff2fe51 --- /dev/null +++ b/public/account/login/twitch.php @@ -0,0 +1,146 @@ +prepare("SELECT id, user_id FROM connections WHERE alias_id = :alias_id AND platform = 'twitch'"); +$stmt->bindValue("alias_id", $twitch_user["id"]); + +$results = $stmt->execute(); + +$user_id = ""; +$user_secret_key = ""; +$user_name = ""; + +if ($row = $results->fetchArray()) { + $id = $row["id"]; + $user_id = $row["user_id"]; + + $stmt = $db->prepare("SELECT * FROM users WHERE id = :id"); + $stmt->bindValue(":id", $id); + $results = $stmt->execute(); + + if ($row = $results->fetchArray()) { + $user_name = $row["username"]; + $user_secret_key = $row["secret_key"]; + $user_id = $row["id"]; + } else { + $db->close(); + echo "Connection found, but not user?"; + exit; + } +} else { + $user_secret_key = generate_random_string(32); + $user_name = $twitch_user["login"]; + + $stmt = $db->prepare("INSERT INTO users(username, secret_key) VALUES (:username, :secret_key)"); + $stmt->bindValue(":username", $user_name); + $stmt->bindValue(":secret_key", $user_secret_key); + if (!$stmt->execute()) { + $db->close(); + echo "Failed to create a user"; + exit; + } + + $user_id = $db->lastInsertRowID(); + + $stmt = $db->prepare("INSERT INTO connections(user_id, alias_id, platform, data) VALUES (:user_id, :alias_id, 'twitch', :data)"); + $stmt->bindValue(":user_id", $user_id); + $stmt->bindValue(":alias_id", $twitch_user["id"]); + $stmt->bindValue( + ":data", + $_SESSION["twitch_access_token"] . ":" . $_SESSION["twitch_refresh_token"] . ":" . $_SESSION["twitch_expires_on"] + ); + $stmt->execute(); +} + +$_SESSION["user_id"] = $user_id; +$_SESSION["user_name"] = $user_name; +setcookie("secret_key", $user_secret_key, time() + 86400 * 30, "/"); + +$db->close(); + +// downloading profile picture +$path = "../../static/userdata/avatars"; + +if (!is_dir($path)) { + mkdir($path, 0777, true); +} + +$fp = fopen("$path/$user_id", "wb"); +$request = curl_init(); +curl_setopt($request, CURLOPT_URL, $twitch_user["profile_image_url"]); +curl_setopt($request, CURLOPT_FILE, $fp); +curl_setopt($request, CURLOPT_HEADER, 0); + +curl_exec($request); +curl_close($request); +fclose($fp); + +header("Location: /account"); \ No newline at end of file diff --git a/public/account/signout.php b/public/account/signout.php new file mode 100644 index 0000000..dd1d0f9 --- /dev/null +++ b/public/account/signout.php @@ -0,0 +1,25 @@ +prepare("UPDATE users SET secret_key = :secret_key WHERE id = :id"); +$stmt->bindValue(":id", $_SESSION["user_id"]); +$stmt->bindValue(":secret_key", generate_random_string(32)); +$stmt->execute(); + +session_unset(); +session_destroy(); + +setcookie("secret_key", "", time() - 1000); + +$db->close(); + +header("Location: /account"); \ No newline at end of file -- cgit v1.2.3