From 3a264df20fdd927fb38d661c2e4803f35b08956a Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Sun, 18 Jul 2021 13:16:00 -0400 Subject: [PATCH] Serve frontend code --- Cargo.toml | 2 +- frontend/loginchange.html | 4 ++-- frontend/loginchange.js | 2 +- frontend/register.js | 4 ++-- src/auth.rs | 27 +++++++++++++++++++++------ src/chat.rs | 4 ++-- src/main.rs | 5 ++++- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ae04496..3b79f4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,5 @@ uuid = { version = "0.8.1", features = ["serde", "v4"] } log = "0.4.0" env_logger = "0.8.4" chrono = { version = "0.4.11", features = ["serde"] } -rocket_contrib = { version = "0.4.10", default-features = false, features = ["json"] } +rocket_contrib = { version = "0.4.10", default-features = false, features = ["json", "serve"] } once_cell = "1.8.0" diff --git a/frontend/loginchange.html b/frontend/loginchange.html index 4fd815a..ace887c 100644 --- a/frontend/loginchange.html +++ b/frontend/loginchange.html @@ -25,7 +25,7 @@


-
+



@@ -38,4 +38,4 @@
- \ No newline at end of file + diff --git a/frontend/loginchange.js b/frontend/loginchange.js index 466beb2..3da1d64 100644 --- a/frontend/loginchange.js +++ b/frontend/loginchange.js @@ -60,5 +60,5 @@ const rawResponse = await fetch(`/api/users/change/${uname}/${pin}/${newUname}/$ body: "" }); document.querySelector("#incorrect").innerHTML = 'Login Changed!' -window.location.replace("http://127.0.0.1:5500/login.html") +window.location.replace("/login.html") } diff --git a/frontend/register.js b/frontend/register.js index fa9f3d6..daff33a 100644 --- a/frontend/register.js +++ b/frontend/register.js @@ -36,7 +36,7 @@ form.addEventListener("submit", async function(event) { }) async function getUname() { - let response = await fetch(`$/api/users/${uname}`); + let response = await fetch(`/api/users/${uname}`); responseText = await response.text(); return responseText; } @@ -50,6 +50,6 @@ const rawResponse = await fetch(`/api/register/${uname.toString().toLowerCase()} body: "" }); document.querySelector("#taken").innerHTML = 'Registered!' -window.location.replace("http://127.0.0.1:5500/login.html") +window.location.replace("/login.html") } diff --git a/src/auth.rs b/src/auth.rs index 898d0d5..c5628d2 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,7 +1,8 @@ extern crate log; use crate::file_io::{append_json, read_json, write_json}; +use rocket::http::{Cookie, Cookies}; use crate::user::User; -use rocket_contrib::json::{Json, JsonValue}; +use rocket_contrib::{json::Json, json::JsonValue, serve::StaticFiles}; extern crate sha1; #[get("/")] @@ -22,7 +23,7 @@ pub fn index() -> &'static str { } // Post request to register a user and pin -#[post("/api/register///")] +#[post("/register///")] pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue { let mut users: Vec = read_json(); // Create an array of users out of parsed json for i in &users { @@ -46,7 +47,7 @@ pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue { }); // append the user to the vec // append to the json file - match append_json(&users) { + match append_json(&users[users.len()-1]) { Err(why) => panic!("couldn't append json: {}", why), Ok(()) => info!("Succesfully appended to json"), }; @@ -62,8 +63,21 @@ pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue { }); } +fn create_token(name: String, mut users: Vec) -> String { + for i in 0..users.len() { + if users[i].name == name { + users[i].session_token = "token".to_string(); + append_json(&users[i]); + info!("succesfully created token for user {}", name); + let token = users[i].session_token.clone(); + return token; + }; + }; + return "NULL".to_string(); +} + // Check if pin matches user -#[get("/api/users//")] +#[get("/users//")] pub fn check_pin(name: String, pin: i32) -> JsonValue { let users: Vec = read_json(); let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string(); @@ -72,6 +86,7 @@ pub fn check_pin(name: String, pin: i32) -> JsonValue { if i.name == name.to_lowercase() { if i.pin_hashed == hashed_pin_input { info!("pin correct for user {}", i.name); + // Create token for user & set a cookie return json!({ "status": "ok", "reason": "pin matches", @@ -96,7 +111,7 @@ pub fn check_pin(name: String, pin: i32) -> JsonValue { } // Change a users pin/name -#[post("/api/users/change////")] +#[post("/users/change////")] pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> JsonValue { let mut users: Vec = read_json(); @@ -174,7 +189,7 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> JsonVal }); } -#[get("/api/users/")] +#[get("/users/")] pub fn get_user(name: String) -> JsonValue { let users: Vec = read_json(); let found_user = users diff --git a/src/chat.rs b/src/chat.rs index c96c33c..1d7909c 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -11,7 +11,7 @@ use crate::user::User; static MESSAGES: Lazy>> = Lazy::new(|| Mutex::new(Vec::new())); -#[get("/api/message/messages.json")] +#[get("/message/messages.json")] pub fn fetch_messages() -> Json> { let messages = { let messages = MESSAGES.lock().unwrap(); @@ -87,7 +87,7 @@ fn check_token(message: Json) -> JsonValue { } // Receive a basic message -#[post("/api/message/send", format = "json", data = "")] +#[post("/message/send", format = "json", data = "")] pub fn send_message(message: Json>) -> JsonValue { check_token(message) } diff --git a/src/main.rs b/src/main.rs index 3814f0e..c7fd3ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate log; extern crate rocket; #[macro_use] extern crate rocket_contrib; +use rocket_contrib::serve::StaticFiles; use rocket::fairing::AdHoc; @@ -25,7 +26,7 @@ fn main() { rocket::ignite() .mount( - "/", + "/api", routes![ auth::index, auth::get_user, @@ -36,6 +37,8 @@ fn main() { chat::fetch_messages ], ) + .mount("/", routes![auth::index]) + .mount("/", StaticFiles::from("frontend")) .attach(cors_fairing) .launch(); }