add auth persistence

This commit is contained in:
2026-01-12 15:59:04 +01:00
parent 76281892a2
commit b756204514
5 changed files with 836 additions and 262 deletions
+9 -2
View File
@@ -1,3 +1,4 @@
use crate::auth_persistence::AuthStorage;
use crate::items;
use crate::proto_utils::Proto;
use rocket::State;
@@ -8,12 +9,16 @@ use uuid::Uuid;
pub struct AuthState {
// Map token -> username
tokens: Mutex<HashMap<String, String>>,
storage: AuthStorage,
}
impl AuthState {
pub fn new() -> Self {
let storage = AuthStorage::new();
let tokens = storage.load_tokens();
Self {
tokens: Mutex::new(HashMap::new()),
tokens: Mutex::new(tokens),
storage,
}
}
}
@@ -138,7 +143,8 @@ pub async fn login(
{
let token = Uuid::new_v4().to_string();
let mut tokens = state.tokens.lock().await;
tokens.insert(token.clone(), req.username);
tokens.insert(token.clone(), req.username.clone());
state.storage.save_tokens(&tokens);
return items::LoginResponse {
token,
@@ -163,6 +169,7 @@ pub async fn logout(
let mut tokens = state.tokens.lock().await;
if tokens.remove(&req.token).is_some() {
state.storage.save_tokens(&tokens);
items::LogoutResponse {
success: true,
message: "Logged out successfully".to_string(),