Update password endpoint #8
+92
-72
@@ -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<uuid::Uuid>
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<sqlx::PgPool>,
|
||||
axum::Json(payload): axum::Json<request::UpdatePasswordRequest>,
|
||||
) -> (axum::http::StatusCode, axum::Json<response::UpdatePasswordResponse>) {
|
||||
) -> (
|
||||
axum::http::StatusCode,
|
||||
axum::Json<response::UpdatePasswordResponse>,
|
||||
) {
|
||||
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<bool, std::io::Error> {
|
||||
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(),
|
||||
}))
|
||||
}
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -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(())
|
||||
|
||||
+2
-1
@@ -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(())
|
||||
|
||||
Reference in New Issue
Block a user