From c929cdceb129601af12d0a01dc2993352bce6e0c Mon Sep 17 00:00:00 2001 From: CatAClock Date: Sun, 22 Jun 2025 22:16:39 -0700 Subject: [PATCH] Basic login system. Kind of. --- src/API/mod.rs | 34 +++++++++++++++++++++++++++++++++- src/HTTP/404.html | 23 ++++++++++------------- src/HTTP/index.html | 28 ++++++++++++++-------------- src/HTTP/profile.html | 42 +++++++++++++++++++++++++++++------------- src/HTTP/profile.js | 26 ++++++++++++++++++++++++++ src/HTTP/script.js | 0 src/HTTP/style.css | 40 +++++++++++++++++++++++++++++++++++++++- src/main.rs | 25 ++++++++++++++++--------- 8 files changed, 167 insertions(+), 51 deletions(-) create mode 100644 src/HTTP/profile.js delete mode 100644 src/HTTP/script.js diff --git a/src/API/mod.rs b/src/API/mod.rs index 32f05da..23a6bdf 100644 --- a/src/API/mod.rs +++ b/src/API/mod.rs @@ -1,6 +1,38 @@ use postgres::{Client, NoTls}; -pub fn GetAccount(ID: i32) -> Result>{ +pub fn GetAccountWithName(Name: &str) -> Result>{ + let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?; + + let Table = Client.query("SELECT 1 + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_TYPE='BASE TABLE' + AND TABLE_NAME='person'", &[]); + // Check if the table doesn't exists. Or does? + match Table { + Ok(_) => { + // Check if the table exists. + if Table?.len() == 0 { + Client.batch_execute(" + CREATE TABLE person ( + id SERIAL PRIMARY KEY, + type TEXT NOT NULL, + name TEXT NOT NULL + )")?; + } + }, + Err(_) => () + } + if Client.query("SELECT name FROM person WHERE name = $1", &[&Name])?.len() != 0 { + let Result: Result = Client.query("SELECT name FROM person WHERE name = $1", &[&Name])?[0].try_get(0); + match Result { + Ok(x) => return Ok(x.to_string()), + Err(_) => return Ok("Other Error Encountered.".to_string()) + } + } + return Ok("Username Not Found.".to_string()); +} + +pub fn GetAccountWithID(ID: i32) -> Result>{ let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?; let Table = Client.query("SELECT 1 diff --git a/src/HTTP/404.html b/src/HTTP/404.html index 2aa1a7a..8c82799 100644 --- a/src/HTTP/404.html +++ b/src/HTTP/404.html @@ -1,16 +1,13 @@ - - - - - Ouch! - - -

404

-

Page not found.

- - - - + + + + + Ouch! + + +

404

+

Page not found.

+ diff --git a/src/HTTP/index.html b/src/HTTP/index.html index b8ec892..76d1181 100644 --- a/src/HTTP/index.html +++ b/src/HTTP/index.html @@ -1,17 +1,17 @@ - - - - - Main - - -

Hello!

-

Hi from Rust.

- View your profile. - - - - + + + + + Main + + +

Hello!

+

Hi from Rust.

+ View your profile. + + + + diff --git a/src/HTTP/profile.html b/src/HTTP/profile.html index 633cf7c..883ac51 100644 --- a/src/HTTP/profile.html +++ b/src/HTTP/profile.html @@ -1,16 +1,32 @@ - - - - - Profile - - -

Profile!

-

This is totally your profile girl.

- - - - + + + + + Profile + + +

Profile!

+

Log in. Or don't. Up to you.

+ +
+
+ + +
+
+ + +
+
+ +
+
+ +

+ + + + diff --git a/src/HTTP/profile.js b/src/HTTP/profile.js new file mode 100644 index 0000000..defe3e8 --- /dev/null +++ b/src/HTTP/profile.js @@ -0,0 +1,26 @@ +let Infomation = document.getElementById("Information"); +let Account = document.getElementById("Account"); + +let req = new XMLHttpRequest(); +req.open('GET', document.location, true); +req.send(null); +req.onload = function() { + let headers = req.getAllResponseHeaders(); + const arr = headers.trim().split(/[\r\n]+/); + + // Create a map of header names to values + const headerMap = new Map(); + arr.forEach((line) => { + const parts = line.split(": "); + headerMap.set(parts[0], parts[1]); + }); + + if (headerMap.get("profile") != null) { + if (headerMap.get("profile") == "Username Not Found.") { + Infomation.innerHTML = "User not found. Please check your username and try again!"; + } else { + Account.innerHTML = headerMap.get("profile"); + Infomation.innerHTML = "Welcome!"; + } + } +}; diff --git a/src/HTTP/script.js b/src/HTTP/script.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/HTTP/style.css b/src/HTTP/style.css index 5f1a1d2..1001b4e 100644 --- a/src/HTTP/style.css +++ b/src/HTTP/style.css @@ -10,6 +10,44 @@ h1 { margin-bottom: 0; } -p { +h2 { + font-size: 7ch; + margin-top: 0; + margin-bottom: 0; +} + +h3 { + font-size: 6ch; + + margin-top: 0; + margin-bottom: 0; +} + +h4 { + font-size: 5ch; + + margin-top: 0; + margin-bottom: 0; +} + +h5 { + font-size: 4ch; + + margin-top: 0; + margin-bottom: 0; +} + +h6 { + font-size: 3ch; + + margin-top: 0; + margin-bottom: 0; +} + +p { + font-size: 2ch; + + margin-top: 0; + margin-bottom: 0; } diff --git a/src/main.rs b/src/main.rs index d31dfb8..13a7c99 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,25 +16,32 @@ fn main(){ Response::from_file("text/html", File::open("src/HTTP/index.html").unwrap()).with_status_code(200) }, (GET) ["/Profile"] => { - Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200) + let Username = Request.get_param("Username"); + let _Password = Request.get_param("Password"); + match Username { + Some(x) => { + let Name = API::GetAccountWithName(&x).unwrap(); + return Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200).with_additional_header("profile", Name); + }, + None => { + return Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200); + } + } }, // API stuff. (GET) ["/API/Profile:Get"] => { // Content-type: application/x-www-form-urlencoded - let Profile = try_or_400!(post_input!(Request, { - ID: i32, - })); - let Things = API::GetAccount(Profile.ID).unwrap(); + let Profile = Request.get_param("id").unwrap().parse::().unwrap(); + let Things = API::GetAccountWithID(Profile).unwrap(); let Text: String = "Got Account: ".to_string() + &Things.to_string(); Response::text(Text).with_status_code(200) }, (POST) ["/API/Profile:Create"] => { // Content-type: application/x-www-form-urlencoded let Profile = try_or_400!(post_input!(Request, { - Type: String, Name: String, })); - let Things = API::MakeAccount(Profile.Type, Profile.Name).unwrap(); + let Things = API::MakeAccount("Person".to_string(), Profile.Name).unwrap(); let Text: String = Things.to_string(); Response::text(Text).with_status_code(201) }, @@ -45,8 +52,8 @@ fn main(){ (GET) ["/style.css"] => { Response::from_file("text/css", File::open("src/HTTP/style.css").unwrap()).with_status_code(200) }, - (GET) ["/script.js"] => { - Response::from_file("text/javascript", File::open("src/HTTP/script.js").unwrap()).with_status_code(200) + (GET) ["/profile.js"] => { + Response::from_file("text/javascript", File::open("src/HTTP/profile.js").unwrap()).with_status_code(200) }, // Catch-all. Fuck you. _ => {