feat: Add frontend 401 unauthorized handling and update backend to async Rocket main.

This commit is contained in:
code002lover 2025-12-03 12:16:49 +01:00
parent 926926f357
commit f9a4dd0851
4 changed files with 48 additions and 4 deletions

33
Cargo.lock generated
View File

@ -105,6 +105,7 @@ name = "backend"
version = "0.1.0"
dependencies = [
"bcrypt",
"bincode",
"bytes",
"prost",
"prost-build",
@ -139,6 +140,26 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72"
[[package]]
name = "bincode"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740"
dependencies = [
"bincode_derive",
"serde",
"unty",
]
[[package]]
name = "bincode_derive"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09"
dependencies = [
"virtue",
]
[[package]]
name = "bitflags"
version = "2.10.0"
@ -2012,6 +2033,12 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "unty"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae"
[[package]]
name = "uuid"
version = "1.18.1"
@ -2035,6 +2062,12 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "virtue"
version = "0.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"

View File

@ -11,6 +11,7 @@ bytes = "1"
rocket_prost_responder_derive = { path = "rocket_prost_responder_derive" }
uuid = { version = "1.10.0", features = ["v4"] }
bcrypt = "0.17.1"
bincode = "2.0.1"
[build-dependencies]
prost-build = "0.14.1"

View File

@ -1,3 +1,4 @@
use bincode::{Decode, Encode};
use rocket::fs::FileServer;
use std::sync::Mutex;
@ -31,7 +32,7 @@ impl std::ops::Deref for User {
fn get_user(
_token: auth::Token,
user_list: &rocket::State<Mutex<Vec<User>>>,
name: String,
name: &str,
) -> Option<items::Person> {
let users = user_list.lock().unwrap();
users
@ -55,7 +56,7 @@ fn get_users(
fn get_game(
_token: auth::Token,
game_list: &rocket::State<Vec<Game>>,
title: String,
title: &str,
) -> Option<items::Game> {
game_list.iter().find(|g| g.title == title).cloned()
}
@ -102,8 +103,8 @@ async fn index_fallback() -> Option<rocket::fs::NamedFile> {
None
}
#[launch]
fn rocket() -> _ {
#[rocket::main]
async fn main() -> Result<(), std::io::Error> {
let mut game_list: Vec<Game> = Vec::new();
let mut user_list: Vec<User> = Vec::new();
@ -139,4 +140,9 @@ fn rocket() -> _ {
)
.mount("/", routes![index_fallback])
.mount("/", FileServer::new("frontend/dist"))
.launch()
.await
.map_err(|e| std::io::Error::other(e.to_string()))?;
Ok(())
}

View File

@ -17,6 +17,10 @@ export const apiFetch = async (
const response = await fetch(url, config);
if (!response.ok) {
if (response.status == 401) {
localStorage.removeItem("token");
window.location.href = "/";
}
throw new Error(`Request failed with status ${response.status}`);
}