From a0652f93c60ca602fb304b2a66a158c50cb148b2 Mon Sep 17 00:00:00 2001 From: CatAClock Date: Sun, 22 Jun 2025 23:10:27 -0700 Subject: [PATCH] you can now login --- src/API/mod.rs | 37 +++++++++++++++++++++---------------- src/HTTP/profile.js | 2 ++ src/main.rs | 13 +++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/API/mod.rs b/src/API/mod.rs index 23a6bdf..d0bd225 100644 --- a/src/API/mod.rs +++ b/src/API/mod.rs @@ -1,6 +1,6 @@ use postgres::{Client, NoTls}; -pub fn GetAccountWithName(Name: &str) -> Result>{ +pub fn Login(Username: &str, Password: &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 @@ -15,21 +15,26 @@ pub fn GetAccountWithName(Name: &str) -> Result () } - 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()) + 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 { + let Result = Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?[0].clone(); + Response.push(Result.get(0)); + Response.push(Result.get(1)); + return Ok(Response); + } else { + return Ok(vec!["Password Not Correct.".to_string()]); } + } else { + return Ok(vec!["Username Not Found.".to_string()]); } - return Ok("Username Not Found.".to_string()); } pub fn GetAccountWithID(ID: i32) -> Result>{ @@ -47,18 +52,18 @@ pub fn GetAccountWithID(ID: i32) -> Result>{ Client.batch_execute(" CREATE TABLE person ( id SERIAL PRIMARY KEY, - type TEXT NOT NULL, - name TEXT NOT NULL + username TEXT NOT NULL, + password TEXT NOT NULL )")?; } }, Err(_) => () } - let Result: String = Client.query("SELECT name FROM person WHERE id = $1", &[&ID]).unwrap()[0].get(0); + 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(Type: String, Name: String) -> Result>{ +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 @@ -73,13 +78,13 @@ pub fn MakeAccount(Type: String, Name: String) -> Result () } - Client.execute("INSERT INTO person (type, name) VALUES ($1, $2)", &[&Type, &Name])?; + Client.execute("INSERT INTO person (username, password) VALUES ($1, $2)", &[&User, &Pass])?; return Ok("Account Created!".to_string()); } diff --git a/src/HTTP/profile.js b/src/HTTP/profile.js index defe3e8..eac77a8 100644 --- a/src/HTTP/profile.js +++ b/src/HTTP/profile.js @@ -18,6 +18,8 @@ req.onload = function() { 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 if (headerMap.get("profile") == "Password Not Correct.") { + Infomation.innerHTML = "Password not correct. Please check your password and try again!"; } else { Account.innerHTML = headerMap.get("profile"); Infomation.innerHTML = "Welcome!"; diff --git a/src/main.rs b/src/main.rs index 13a7c99..a374be3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,14 +17,14 @@ fn main(){ }, (GET) ["/Profile"] => { let Username = Request.get_param("Username"); - let _Password = Request.get_param("Password"); + let Password = Request.get_param("Password").unwrap(); 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); + let Account: Vec = API::Login(&x, &Password).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 => { - return Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200); + Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200) } } }, @@ -39,9 +39,10 @@ fn main(){ (POST) ["/API/Profile:Create"] => { // Content-type: application/x-www-form-urlencoded let Profile = try_or_400!(post_input!(Request, { - Name: String, + Username: String, + Password: String, })); - let Things = API::MakeAccount("Person".to_string(), Profile.Name).unwrap(); + 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) },