You can now sign up and sign in!

This commit is contained in:
CatAClock 2025-06-23 12:33:19 -07:00
parent a0652f93c6
commit c60fb18003
5 changed files with 73 additions and 78 deletions

View file

@ -1,12 +1,12 @@
use postgres::{Client, NoTls}; use postgres::{Client, NoTls};
pub fn Login(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std::error::Error>>{ fn CheckTableIsHere(Table: &str) -> Result<(), 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
FROM INFORMATION_SCHEMA.TABLES FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE' WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='person'", &[]); AND TABLE_NAME='$1'", &[&Table]);
// Check if the table doesn't exists. Or does? // Check if the table doesn't exists. Or does?
match Table { match Table {
Ok(_) => { Ok(_) => {
@ -21,7 +21,16 @@ pub fn Login(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std:
} }
}, },
Err(_) => () Err(_) => ()
} };
Ok(())
}
pub fn Signin(Username: &str, Password: &str) -> Result<Vec<String>, Box<dyn std::error::Error>>{
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 { if Client.query("SELECT username, password FROM person WHERE username = $1", &[&Username])?.len() != 0 {
let mut Response: Vec<String> = Vec::new(); let mut Response: Vec<String> = Vec::new();
if Client.query("SELECT username, password FROM person WHERE password = $1", &[&Password])?.len() != 0 { 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<Vec<String>, Box<dyn std:
} }
} }
pub fn GetAccountWithID(ID: i32) -> Result<String, Box<dyn std::error::Error>>{ pub fn Signup(Username: &str, Password: &str) -> Result<(), Box<dyn std::error::Error>>{
let _ = CheckTableIsHere("person");
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 Client.execute("INSERT INTO person (username, password) VALUES ($1, $2)", &[&Username, &Password])?;
FROM INFORMATION_SCHEMA.TABLES println!("Account Created!");
WHERE TABLE_TYPE='BASE TABLE' return Ok(());
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<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,
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());
} }

View file

@ -8,9 +8,9 @@
</head> </head>
<body> <body>
<h1>Profile!</h1> <h1>Profile!</h1>
<p id="Account">Log in. Or don't. Up to you.</p> <p id="Account">Sign in. Or don't. Up to you.</p>
<a href="/Profile/Signup">Sign Up!</a>
<form action="" method="get"> <form action="" method="GET" id="Login">
<div> <div>
<label>Username</label> <label>Username</label>
<input type="text" name="Username" required /> <input type="text" name="Username" required />
@ -20,13 +20,13 @@
<input type="text" name="Password" required /> <input type="text" name="Password" required />
</div> </div>
<div> <div>
<input type="submit" value="Log on" /> <input type="submit" value="Sign In" />
</div> </div>
</form> </form>
<p id="Information"></p> <p id="Information"></p>
<script src="profile.js"></script> <script src="/profile.js"></script>
</body> </body>
</html> </html>

View file

@ -15,6 +15,8 @@ req.onload = function() {
headerMap.set(parts[0], parts[1]); headerMap.set(parts[0], parts[1]);
}); });
console.log(headerMap);
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!";

30
src/HTTP/signup.html Normal file
View file

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="icon" href="/favicon.ico" type="image/webp" />
<link rel="stylesheet" href="/style.css" />
<title>Profile</title>
</head>
<body>
<h1>Profile!</h1>
<p id="Account">Sign Up. Or don't. Up to you.</p>
<form action="" method="GET" id="Signup">
<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="Sign Up" />
</div>
</form>
<p id="Information"></p>
</body>
</html>

View file

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