you can now login

This commit is contained in:
CatAClock 2025-06-22 23:10:27 -07:00
parent c929cdceb1
commit a0652f93c6
3 changed files with 30 additions and 22 deletions

View file

@ -1,6 +1,6 @@
use postgres::{Client, NoTls}; use postgres::{Client, NoTls};
pub fn GetAccountWithName(Name: &str) -> Result<String, Box<dyn std::error::Error>>{ pub fn Login(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std::error::Error>>{
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?; let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let Table = Client.query("SELECT 1 let Table = Client.query("SELECT 1
@ -15,21 +15,26 @@ pub fn GetAccountWithName(Name: &str) -> Result<String, Box<dyn std::error::Erro
Client.batch_execute(" Client.batch_execute("
CREATE TABLE person ( CREATE TABLE person (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
type TEXT NOT NULL, username TEXT NOT NULL,
name TEXT NOT NULL password TEXT NOT NULL
)")?; )")?;
} }
}, },
Err(_) => () Err(_) => ()
} }
if Client.query("SELECT name FROM person WHERE name = $1", &[&Name])?.len() != 0 { if Client.query("SELECT username, password FROM person WHERE username = $1", &[&Username])?.len() != 0 {
let Result: Result<String, postgres::Error> = Client.query("SELECT name FROM person WHERE name = $1", &[&Name])?[0].try_get(0); let mut Response: Vec<String> = Vec::new();
match Result { if Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?.len() != 0 {
Ok(x) => return Ok(x.to_string()), let Result = Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?[0].clone();
Err(_) => return Ok("Other Error Encountered.".to_string()) 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<String, Box<dyn std::error::Error>>{ pub fn GetAccountWithID(ID: i32) -> Result<String, Box<dyn std::error::Error>>{
@ -47,18 +52,18 @@ pub fn GetAccountWithID(ID: i32) -> Result<String, Box<dyn std::error::Error>>{
Client.batch_execute(" Client.batch_execute("
CREATE TABLE person ( CREATE TABLE person (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
type TEXT NOT NULL, username TEXT NOT NULL,
name TEXT NOT NULL password TEXT NOT NULL
)")?; )")?;
} }
}, },
Err(_) => () 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()); return Ok(Result.to_string());
} }
pub fn MakeAccount(Type: String, Name: String) -> Result<String, Box<dyn std::error::Error>>{ pub fn MakeAccount(User: String, Pass: String) -> Result<String, Box<dyn std::error::Error>>{
let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?; let mut Client = Client::connect("host=/var/run/postgresql,localhost user=postgres password=Password dbname=ActivityPub", NoTls)?;
let Table = Client.query("SELECT 1 let Table = Client.query("SELECT 1
@ -73,13 +78,13 @@ pub fn MakeAccount(Type: String, Name: String) -> Result<String, Box<dyn std::er
Client.batch_execute(" Client.batch_execute("
CREATE TABLE person ( CREATE TABLE person (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
type TEXT NOT NULL, username TEXT NOT NULL,
name TEXT NOT NULL password TEXT NOT NULL
)")?; )")?;
} }
}, },
Err(_) => () Err(_) => ()
} }
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()); return Ok("Account Created!".to_string());
} }

View file

@ -18,6 +18,8 @@ req.onload = function() {
if (headerMap.get("profile") != null) { if (headerMap.get("profile") != null) {
if (headerMap.get("profile") == "Username Not Found.") { if (headerMap.get("profile") == "Username Not Found.") {
Infomation.innerHTML = "User not found. Please check your username and try again!"; 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 { } else {
Account.innerHTML = headerMap.get("profile"); Account.innerHTML = headerMap.get("profile");
Infomation.innerHTML = "Welcome!"; Infomation.innerHTML = "Welcome!";

View file

@ -17,14 +17,14 @@ fn main(){
}, },
(GET) ["/Profile"] => { (GET) ["/Profile"] => {
let Username = Request.get_param("Username"); let Username = Request.get_param("Username");
let _Password = Request.get_param("Password"); let Password = Request.get_param("Password").unwrap();
match Username { match Username {
Some(x) => { Some(x) => {
let Name = API::GetAccountWithName(&x).unwrap(); let Account: Vec<String> = API::Login(&x, &Password).unwrap();
return Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200).with_additional_header("profile", Name); Response::from_file("text/html", File::open("src/HTTP/profile.html").unwrap()).with_status_code(200).with_additional_header("profile", Account[0].clone())
}, },
None => { 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"] => { (POST) ["/API/Profile:Create"] => {
// Content-type: application/x-www-form-urlencoded // Content-type: application/x-www-form-urlencoded
let Profile = try_or_400!(post_input!(Request, { 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(); let Text: String = Things.to_string();
Response::text(Text).with_status_code(201) Response::text(Text).with_status_code(201)
}, },