Basic login system. Kind of.

This commit is contained in:
CatAClock 2025-06-22 22:16:39 -07:00
parent 1f6176de2e
commit c929cdceb1
8 changed files with 167 additions and 51 deletions

View file

@ -1,6 +1,38 @@
use postgres::{Client, NoTls}; use postgres::{Client, NoTls};
pub fn GetAccount(ID: i32) -> Result<String, Box<dyn std::error::Error>>{ pub fn GetAccountWithName(Name: &str) -> 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 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<String, postgres::Error> = 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<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

View file

@ -1,16 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="icon" href="/favicon.ico" type="image/webp" /> <link rel="icon" href="/favicon.ico" type="image/webp" />
<link rel="stylesheet" href="/style.css" /> <link rel="stylesheet" href="/style.css" />
<title>Ouch!</title> <title>Ouch!</title>
</head> </head>
<body> <body>
<h1>404</h1> <h1>404</h1>
<p>Page not found.</p> <p>Page not found.</p>
</body>
<script src="script.js"></script>
</body>
</html> </html>

View file

@ -1,17 +1,17 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="icon" href="/favicon.ico" type="image/webp" /> <link rel="icon" href="/favicon.ico" type="image/webp" />
<link rel="stylesheet" href="/style.css" /> <link rel="stylesheet" href="/style.css" />
<title>Main</title> <title>Main</title>
</head> </head>
<body> <body>
<h1>Hello!</h1> <h1>Hello!</h1>
<p>Hi from Rust.</p> <p>Hi from Rust.</p>
<a href="/Profile">View your profile.</a> <a href="/Profile">View your profile.</a>
<script src="script.js"></script> <script src="script.js"></script>
</body> </body>
</html> </html>

View file

@ -1,16 +1,32 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<link rel="icon" href="/favicon.ico" type="image/webp" /> <link rel="icon" href="/favicon.ico" type="image/webp" />
<link rel="stylesheet" href="/style.css" /> <link rel="stylesheet" href="/style.css" />
<title>Profile</title> <title>Profile</title>
</head> </head>
<body> <body>
<h1>Profile!</h1> <h1>Profile!</h1>
<p>This is totally your profile girl.</p> <p id="Account">Log in. Or don't. Up to you.</p>
<script src="script.js"></script> <form action="" method="get">
<div>
<label>Username</label>
<input type="text" name="Username" required />
</div>
<div>
<label>Password</label>
<input type="text" name="Password" required />
</div>
<div>
<input type="submit" value="Log on" />
</div>
</form>
</body> <p id="Information"></p>
<script src="profile.js"></script>
</body>
</html> </html>

26
src/HTTP/profile.js Normal file
View file

@ -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!";
}
}
};

View file

View file

@ -10,6 +10,44 @@ h1 {
margin-bottom: 0; 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;
} }

View file

@ -16,25 +16,32 @@ fn main(){
Response::from_file("text/html", File::open("src/HTTP/index.html").unwrap()).with_status_code(200) Response::from_file("text/html", File::open("src/HTTP/index.html").unwrap()).with_status_code(200)
}, },
(GET) ["/Profile"] => { (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. // API stuff.
(GET) ["/API/Profile:Get"] => { (GET) ["/API/Profile:Get"] => {
// Content-type: application/x-www-form-urlencoded // Content-type: application/x-www-form-urlencoded
let Profile = try_or_400!(post_input!(Request, { let Profile = Request.get_param("id").unwrap().parse::<i32>().unwrap();
ID: i32, let Things = API::GetAccountWithID(Profile).unwrap();
}));
let Things = API::GetAccount(Profile.ID).unwrap();
let Text: String = "Got Account: ".to_string() + &Things.to_string(); let Text: String = "Got Account: ".to_string() + &Things.to_string();
Response::text(Text).with_status_code(200) Response::text(Text).with_status_code(200)
}, },
(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, {
Type: String,
Name: 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(); let Text: String = Things.to_string();
Response::text(Text).with_status_code(201) Response::text(Text).with_status_code(201)
}, },
@ -45,8 +52,8 @@ fn main(){
(GET) ["/style.css"] => { (GET) ["/style.css"] => {
Response::from_file("text/css", File::open("src/HTTP/style.css").unwrap()).with_status_code(200) Response::from_file("text/css", File::open("src/HTTP/style.css").unwrap()).with_status_code(200)
}, },
(GET) ["/script.js"] => { (GET) ["/profile.js"] => {
Response::from_file("text/javascript", File::open("src/HTTP/script.js").unwrap()).with_status_code(200) Response::from_file("text/javascript", File::open("src/HTTP/profile.js").unwrap()).with_status_code(200)
}, },
// Catch-all. Fuck you. // Catch-all. Fuck you.
_ => { _ => {