diff --git a/src/main.rs b/src/main.rs index 31fd54c..7ef2bac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -226,14 +226,16 @@ fn oidc_config() -> Json { impl<'a, 'r> FromRequest<'a, 'r> for User { type Error = (); - fn from_request(_request: &'a Request<'r>) -> Outcome { - Outcome::Success(User { - id: 1, - username: "Alex".to_string(), - is_active: true, - created_at: None, - updated_at: None, - }) + fn from_request(request: &'a Request<'r>) -> Outcome { + let mut user_id = match request.cookies().get_private("user_id") { + Some(cookie) => cookie.value().to_string(), + None => return Outcome::Forward(()), + }; + let conn = request.guard::().unwrap(); + match User::get_with_id(&conn, user_id) { + Ok(user) => Outcome::Success(user), + _ => Outcome::Forward(()), + } } } diff --git a/src/models.rs b/src/models.rs index 44eb2cc..44e0610 100644 --- a/src/models.rs +++ b/src/models.rs @@ -25,6 +25,12 @@ pub struct NewUser { } impl User { + pub fn get_with_id(conn: &PgConnection, user_id_str: String) -> QueryResult { + use crate::schema::users::dsl::{ id, users }; + let user_id = user_id_str.parse::().unwrap(); + users.filter(id.eq(&user_id)).first::(conn) + } + pub fn get_with_username(conn: &PgConnection, name: String) -> QueryResult { use crate::schema::users::dsl::{ username, users }; users.filter(username.eq(&name)).first::(conn)