Basic login system. Kind of.
This commit is contained in:
parent
1f6176de2e
commit
c929cdceb1
8 changed files with 167 additions and 51 deletions
|
@ -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
|
||||||
|
|
|
@ -9,8 +9,5 @@
|
||||||
<body>
|
<body>
|
||||||
<h1>404</h1>
|
<h1>404</h1>
|
||||||
<p>Page not found.</p>
|
<p>Page not found.</p>
|
||||||
|
|
||||||
<script src="script.js"></script>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -8,9 +8,25 @@
|
||||||
</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>
|
||||||
|
|
||||||
|
<p id="Information"></p>
|
||||||
|
|
||||||
|
<script src="profile.js"></script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
26
src/HTTP/profile.js
Normal file
26
src/HTTP/profile.js
Normal 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!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
25
src/main.rs
25
src/main.rs
|
@ -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.
|
||||||
_ => {
|
_ => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue