diff --git a/src/callers/login.rs b/src/callers/login.rs index c9030cd..1104cad 100644 --- a/src/callers/login.rs +++ b/src/callers/login.rs @@ -48,7 +48,11 @@ pub mod request { impl UpdatePasswordRequest { pub fn is_valid(&self) -> bool { - if self.user_id.is_nil() || !self.current_password.is_empty() || !self.updated_password.is_empty() || !self.confirmed_password.is_empty() { + if self.user_id.is_nil() + || !self.current_password.is_empty() + || !self.updated_password.is_empty() + || !self.confirmed_password.is_empty() + { false } else { self.updated_password == self.confirmed_password @@ -90,7 +94,7 @@ pub mod response { #[derive(Deserialize, Serialize, utoipa::ToSchema)] pub struct UpdatePasswordResponse { pub message: String, - pub data: Vec + pub data: Vec, } } @@ -534,7 +538,6 @@ pub async fn refresh_token( } } - /// Endpoint for a updating password #[utoipa::path( post, @@ -553,7 +556,10 @@ pub async fn refresh_token( pub async fn update_password( axum::Extension(pool): axum::Extension, axum::Json(payload): axum::Json, -) -> (axum::http::StatusCode, axum::Json) { +) -> ( + axum::http::StatusCode, + axum::Json, +) { if !payload.is_valid() { ( axum::http::StatusCode::BAD_REQUEST, @@ -563,16 +569,13 @@ pub async fn update_password( }), ) } else { - let verify_password = |current_password, hashed_password| -> Result { - match hashing::verify_password(current_password, hashed_password) { - Ok(matches) => { - Ok(matches) - } - Err(err) => Err(std::io::Error::other(err.to_string())), - } + match hashing::verify_password(current_password, hashed_password) { + Ok(matches) => Ok(matches), + Err(err) => Err(std::io::Error::other(err.to_string())), + } }; - + match repo::user::get_with_id(&pool, &payload.user_id).await { Ok(user) => { let hashed_password = user.password.clone(); @@ -581,49 +584,56 @@ pub async fn update_password( if matches { let (generate_salt, mut salt) = super::register::generate_the_salt(); salt.id = repo::salt::insert(&pool, &salt).await.unwrap(); - let updated_hashed_password = match hashing::hash_password(&payload.updated_password, &generate_salt) { - Ok(hashed) => { - hashed - } + let updated_hashed_password = match hashing::hash_password( + &payload.updated_password, + &generate_salt, + ) { + Ok(hashed) => hashed, Err(err) => { eprintln!("Error: {err:?}"); String::new() } }; - match repo::user::update_password(&pool, &user, &updated_hashed_password).await { - Ok(()) => { - (axum::http::StatusCode::OK, + match repo::user::update_password( + &pool, + &user, + &updated_hashed_password, + ) + .await + { + Ok(()) => ( + axum::http::StatusCode::OK, axum::Json(response::UpdatePasswordResponse { message: String::from(super::messages::SUCCESSFUL_MESSAGE), data: vec![user.id], - })) - } - Err(err) => { - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, + }), + ), + Err(err) => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::UpdatePasswordResponse { message: err.to_string(), data: Vec::new(), - })) - } + }), + ), } } else { - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::UpdatePasswordResponse { - message: String::from("Issue updating password"), - data: Vec::new(), - })) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UpdatePasswordResponse { + message: String::from("Issue updating password"), + data: Vec::new(), + }), + ) } } - Err(err) => { - ( - axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::UpdatePasswordResponse { - message: err.to_string(), - data: Vec::new(), - }), - ) - } + Err(err) => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UpdatePasswordResponse { + message: err.to_string(), + data: Vec::new(), + }), + ), } } Err(err) => { @@ -636,60 +646,70 @@ pub async fn update_password( match verify_password(&payload.current_password, hashed_password) { Ok(matches) => { if matches { - let (generate_salt, mut salt) = super::register::generate_the_salt(); + let (generate_salt, mut salt) = + super::register::generate_the_salt(); salt.id = repo::salt::insert(&pool, &salt).await.unwrap(); - let updated_hashed_password = match hashing::hash_password(&payload.updated_password, &generate_salt) { - Ok(hashed) => { - hashed - } + let updated_hashed_password = match hashing::hash_password( + &payload.updated_password, + &generate_salt, + ) { + Ok(hashed) => hashed, Err(err) => { eprintln!("Error: {err:?}"); String::new() } }; - match repo::service::update_passphrase(&pool, &service_user, &updated_hashed_password).await { - Ok(()) => { - (axum::http::StatusCode::OK, + match repo::service::update_passphrase( + &pool, + &service_user, + &updated_hashed_password, + ) + .await + { + Ok(()) => ( + axum::http::StatusCode::OK, axum::Json(response::UpdatePasswordResponse { - message: String::from(super::messages::SUCCESSFUL_MESSAGE), + message: String::from( + super::messages::SUCCESSFUL_MESSAGE, + ), data: vec![service_user.id], - })) - } - Err(err) => { - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, + }), + ), + Err(err) => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::UpdatePasswordResponse { message: err.to_string(), data: Vec::new(), - })) - } + }), + ), } } else { - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::UpdatePasswordResponse { - message: String::from("Issue updating password"), - data: Vec::new(), - })) + ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UpdatePasswordResponse { + message: String::from("Issue updating password"), + data: Vec::new(), + }), + ) } } - Err(err) => { - ( + Err(err) => ( + axum::http::StatusCode::INTERNAL_SERVER_ERROR, + axum::Json(response::UpdatePasswordResponse { + message: err.to_string(), + data: Vec::new(), + }), + ), + } + } + Err(err) => ( axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response::UpdatePasswordResponse { message: err.to_string(), data: Vec::new(), }), - ) - } - } - } - Err(err) => { - (axum::http::StatusCode::INTERNAL_SERVER_ERROR, - axum::Json(response::UpdatePasswordResponse { - message: err.to_string(), - data: Vec::new(), - })) - } + ), } } } diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 373375c..d0ff331 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -117,7 +117,8 @@ pub mod user { .bind(updated_hashed_password) .bind(user.id) .execute(pool) - .await { + .await + { Ok(row) => { if row.rows_affected() > 0 { Ok(()) diff --git a/src/repo/service.rs b/src/repo/service.rs index e1966bf..da80247 100644 --- a/src/repo/service.rs +++ b/src/repo/service.rs @@ -167,7 +167,8 @@ pub async fn update_passphrase( .bind(updated_hashed_passphrase) .bind(user.id) .execute(pool) - .await { + .await + { Ok(row) => { if row.rows_affected() > 0 { Ok(())