From c60fb1800367279347481f5e064e0b584ee6a25a Mon Sep 17 00:00:00 2001 From: CatAClock Date: Mon, 23 Jun 2025 12:33:19 -0700 Subject: [PATCH] You can now sign up and sign in! --- src/{API => Extra}/mod.rs | 69 ++++++++++----------------------------- src/HTTP/profile.html | 10 +++--- src/HTTP/profile.js | 2 ++ src/HTTP/signup.html | 30 +++++++++++++++++ src/main.rs | 40 ++++++++++------------- 5 files changed, 73 insertions(+), 78 deletions(-) rename src/{API => Extra}/mod.rs (51%) create mode 100644 src/HTTP/signup.html diff --git a/src/API/mod.rs b/src/Extra/mod.rs similarity index 51% rename from src/API/mod.rs rename to src/Extra/mod.rs index d0bd225..34a47b8 100644 --- a/src/API/mod.rs +++ b/src/Extra/mod.rs @@ -1,12 +1,12 @@ use postgres::{Client, NoTls}; -pub fn Login(Username: &str, Password: &str) -> Result, Box>{ +fn CheckTableIsHere(Table: &str) -> Result<(), Box> { 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'", &[]); + AND TABLE_NAME='$1'", &[&Table]); // Check if the table doesn't exists. Or does? match Table { Ok(_) => { @@ -21,7 +21,16 @@ pub fn Login(Username: &str, Password: &str) -> Result, Box () - } + }; + Ok(()) +} + +pub fn Signin(Username: &str, Password: &str) -> Result, Box>{ + let _ = CheckTableIsHere("person"); + + let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?; + + // Check to see if the username or password is incorrect. if Client.query("SELECT username, password FROM person WHERE username = $1", &[&Username])?.len() != 0 { let mut Response: Vec = Vec::new(); if Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?.len() != 0 { @@ -37,54 +46,12 @@ pub fn Login(Username: &str, Password: &str) -> Result, Box Result>{ +pub fn Signup(Username: &str, Password: &str) -> Result<(), Box>{ + let _ = CheckTableIsHere("person"); + 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, - username TEXT NOT NULL, - password TEXT NOT NULL - )")?; - } - }, - Err(_) => () - } - let Result: String = Client.query("SELECT username FROM person WHERE id = $1", &[&ID]).unwrap()[0].get(0); - return Ok(Result.to_string()); -} - -pub fn MakeAccount(User: String, Pass: String) -> 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, - username TEXT NOT NULL, - password TEXT NOT NULL - )")?; - } - }, - Err(_) => () - } - Client.execute("INSERT INTO person (username, password) VALUES ($1, $2)", &[&User, &Pass])?; - return Ok("Account Created!".to_string()); + Client.execute("INSERT INTO person (username, password) VALUES ($1, $2)", &[&Username, &Password])?; + println!("Account Created!"); + return Ok(()); } diff --git a/src/HTTP/profile.html b/src/HTTP/profile.html index 883ac51..0bd734a 100644 --- a/src/HTTP/profile.html +++ b/src/HTTP/profile.html @@ -8,9 +8,9 @@

Profile!

-

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

- -
+

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

+ Sign Up! +
@@ -20,13 +20,13 @@
- +

- + diff --git a/src/HTTP/profile.js b/src/HTTP/profile.js index eac77a8..fb31ddc 100644 --- a/src/HTTP/profile.js +++ b/src/HTTP/profile.js @@ -15,6 +15,8 @@ req.onload = function() { headerMap.set(parts[0], parts[1]); }); + console.log(headerMap); + if (headerMap.get("profile") != null) { if (headerMap.get("profile") == "Username Not Found.") { Infomation.innerHTML = "User not found. Please check your username and try again!"; diff --git a/src/HTTP/signup.html b/src/HTTP/signup.html new file mode 100644 index 0000000..c197bb8 --- /dev/null +++ b/src/HTTP/signup.html @@ -0,0 +1,30 @@ + + + + + + + Profile + + +

Profile!

+

Sign Up. Or don't. Up to you.

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

+ + + diff --git a/src/main.rs b/src/main.rs index a374be3..551db5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ #![allow(non_snake_case)] #![allow(unused_braces)] -use rouille::{post_input, try_or_400, router, Response}; +use rouille::{router, Response}; use std::fs::File; mod ActivityPub; -mod API; +mod Extra; fn main(){ - // Never leave the server. CTRL + C if you have issues. rouille::start_server("127.0.0.1:8080", move |Request| { // Router. Go to the correct pages, else hit the sack. @@ -16,11 +15,12 @@ fn main(){ Response::from_file("text/html", File::open("src/HTTP/index.html").unwrap()).with_status_code(200) }, (GET) ["/Profile"] => { + // Signing in. let Username = Request.get_param("Username"); - let Password = Request.get_param("Password").unwrap(); + let Password = Request.get_param("Password"); match Username { Some(x) => { - let Account: Vec = API::Login(&x, &Password).unwrap(); + let Account: Vec = Extra::Signin(&x, &Password.unwrap()).unwrap(); Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200).with_additional_header("profile", Account[0].clone()) }, None => { @@ -28,23 +28,19 @@ fn main(){ } } }, - // API stuff. - (GET) ["/API/Profile:Get"] => { - // Content-type: application/x-www-form-urlencoded - 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, { - Username: String, - Password: String, - })); - let Things = API::MakeAccount(Profile.Username.to_string(), Profile.Password.to_string()).unwrap(); - let Text: String = Things.to_string(); - Response::text(Text).with_status_code(201) + (GET) ["/Profile/Signup"] => { + // Signing up. + let Username = Request.get_param("Username"); + let Password = Request.get_param("Password"); + match Username { + Some(x) => { + let _ = Extra::Signup(&x, &Password.unwrap()).unwrap(); + Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200).with_additional_header("profile", x) + }, + None => { + Response::from_file("text/html", File::open("src/HTTP/signup.html").unwrap()).with_status_code(200) + }, + } }, // Get specific images. Because the browser said so. (GET) ["/favicon.ico"] => {