diff --git a/src/auth/session.rs b/src/auth/session.rs index 5ec5c1c..cac7940 100644 --- a/src/auth/session.rs +++ b/src/auth/session.rs @@ -33,7 +33,7 @@ pub enum SessionKind { } pub trait Sessions { - fn from_session(session: &str) -> impl std::future::Future>; + fn from_session(session: String) -> impl std::future::Future>; fn login( username: &str, password: &str, @@ -78,11 +78,11 @@ impl Sessions for User { } // Get a user from session ID - async fn from_session(session: &str) -> Option { - let user: Option = sqlx::query_as("SELECT * FROM users WHERE username = (SELECT \"user\" FROM user_session WHERE token = $1)").bind(session).fetch_optional(get_pg!()).await.unwrap(); + async fn from_session(session: String) -> Option { + let user: Option = sqlx::query_as("SELECT * FROM users WHERE username = (SELECT \"user\" FROM user_session WHERE token = $1)").bind(&session).fetch_optional(get_pg!()).await.unwrap(); if let Some(mut user) = user { - user.session = session.to_string(); + user.session = session; return Some(user); } diff --git a/src/auth/user.rs b/src/auth/user.rs index 8db3151..dabe42c 100644 --- a/src/auth/user.rs +++ b/src/auth/user.rs @@ -54,14 +54,14 @@ impl User { /// Create a new user with the given details /// /// Returns an Option containing the created user, or None if a user already exists with the same username - pub async fn create(username: &str, password: &str, role: UserRole) -> Option { + pub async fn create(username: String, password: &str, role: UserRole) -> Option { // Check if a user already exists with the same username - if Self::find(username).await.is_some() { + if Self::find(&username).await.is_some() { return None; } let u = Self { - username: username.to_string(), + username, password: bcrypt::hash(password, bcrypt::DEFAULT_COST).unwrap(), user_role: role, session: String::new(), @@ -133,7 +133,7 @@ impl ToAPI for User { /// extracts a user from a request with `session` cookie async fn extract_user(request: &Request<'_>) -> Option { if let Some(session_id) = request.cookies().get("session") { - if let Some(user) = User::from_session(session_id.value()).await { + if let Some(user) = User::from_session(session_id.value().to_string()).await { return Some(user); } return None; @@ -164,7 +164,7 @@ impl<'r> FromRequest<'r> for APIUser { async fn from_request(request: &'r Request<'_>) -> rocket::request::Outcome { match request.headers().get_one("token") { Some(key) => { - if let Some(user) = User::from_session(key).await { + if let Some(user) = User::from_session(key.to_string()).await { return Outcome::Success(APIUser(user)); } return Outcome::Error((Status::Unauthorized, ())); diff --git a/src/request/assets.rs b/src/request/assets.rs index 11ba639..ead84fc 100644 --- a/src/request/assets.rs +++ b/src/request/assets.rs @@ -15,10 +15,10 @@ pub struct DataResponse { impl DataResponse { #[must_use] - pub fn new(data: Vec, content_type: &str, cache_duration: Option) -> Self { + pub fn new(data: Vec, content_type: String, cache_duration: Option) -> Self { Self { data, - content_type: content_type.to_string(), + content_type, cache_duration, } } diff --git a/src/request/mod.rs b/src/request/mod.rs index 6d702fe..6365cde 100644 --- a/src/request/mod.rs +++ b/src/request/mod.rs @@ -67,8 +67,8 @@ pub fn respond_json(json: &serde_json::Value) -> StringResponse { /// # Returns /// A `StringResponse` with status `200 OK`, content type `text/html`, and the HTML content as the body. #[must_use] -pub fn respond_html(html: &str) -> StringResponse { - (Status::Ok, (ContentType::HTML, html.to_string())) +pub fn respond_html(html: String) -> StringResponse { + (Status::Ok, (ContentType::HTML, html)) } /// Helper function to create an JS HTTP response. @@ -79,8 +79,8 @@ pub fn respond_html(html: &str) -> StringResponse { /// # Returns /// A `StringResponse` with status `200 OK`, content type `text/javascript`, and the JS content as the body. #[must_use] -pub fn respond_script(script: &str) -> StringResponse { - (Status::Ok, (ContentType::JavaScript, script.to_string())) +pub fn respond_script(script: String) -> StringResponse { + (Status::Ok, (ContentType::JavaScript, script)) } /// Creates a custom HTTP response with the specified status, content type, and body.