diff --git a/src/auth/session.rs b/src/auth/session.rs index 8c0fa4a..7dac4cf 100644 --- a/src/auth/session.rs +++ b/src/auth/session.rs @@ -1,24 +1,30 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::{Arc, Mutex}}; use serde::{Deserialize, Serialize}; pub struct Sessions { - sessions: HashMap, + sessions: Mutex>, } impl Sessions { pub fn new() -> Self { Sessions { - sessions: HashMap::new(), + sessions: Mutex::new(HashMap::new()), } } - pub fn insert(&mut self, session_id: String, session_data: SessionData) { - self.sessions.insert(session_id, session_data); + pub fn insert(&self, session_id: String, session_data: SessionData) { + let mut map = self.sessions.lock().unwrap(); + map.insert(session_id, session_data); } - pub fn get(&self, session_id: &str) -> Option<&SessionData> { - self.sessions.get(session_id) + pub fn get(&self, session_id: &str) -> Option { + let map = self.sessions.lock().unwrap(); + + match map.get(session_id) { + Some(s) => Some(s.clone()), + None => None, + } } } @@ -26,3 +32,9 @@ impl Sessions { pub struct SessionData { user_id: i32, } + +impl SessionData { + pub fn new(user_id: i32) -> Self { + SessionData { user_id } + } +} diff --git a/src/controller/login.rs b/src/controller/login.rs new file mode 100644 index 0000000..17b42c2 --- /dev/null +++ b/src/controller/login.rs @@ -0,0 +1,20 @@ +use maud::Markup; +use rocket::{form::Form, http::Status, State}; + +use crate::auth::session::{SessionData, Sessions}; + +#[derive(FromForm)] +pub struct LoginData { + email: String, + password: String, +} + +#[get("/login")] +pub fn login(sessions: &State) -> &'static str { + sessions.insert( + "123456".into(), + SessionData::new(1), + ); + + ":D" +} diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 271526b..1bf2123 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -1,5 +1,6 @@ use maud::Markup; +pub mod login; pub mod register; pub mod user; diff --git a/src/main.rs b/src/main.rs index 28c3224..e0c567b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,9 @@ fn rocket() -> _ { .manage(auth::session::Sessions::new()) .register("/", catchers![view::not_authorized]) .mount("/", routes![controller::index,]) - .mount("/f", routes![controller::user::create_user,]) + .mount("/f", routes![ + controller::user::create_user, + controller::login::login, + ]) .mount("/static", FileServer::from("static")) }