-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85ff9ce
commit a12327c
Showing
4 changed files
with
88 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,49 @@ | ||
use diesel::prelude::*; | ||
use tracing::{error, info}; | ||
use uuid::Uuid; | ||
|
||
use crate::{data::models::User, enums::errors::internal::Result}; | ||
use crate::{ | ||
data::schema, | ||
enums::errors::internal::{Auth, Error, Result}, | ||
utils::hash, | ||
}; | ||
|
||
use super::get_by_id; | ||
|
||
pub async fn change_password( | ||
_pool: &deadpool_diesel::postgres::Pool, | ||
_user_id: &Uuid, | ||
_new_password: &str, | ||
) -> Result<User> { | ||
todo!() | ||
pool: &deadpool_diesel::postgres::Pool, | ||
user_id: &Uuid, | ||
new_password: &str, | ||
) -> Result<()> { | ||
let conn = pool.get().await?; | ||
let user = get_by_id(pool, user_id).await?; | ||
|
||
if user.classic_auth.is_none() { | ||
return Err(Error::Auth(Auth::NoClassicAuth)); | ||
} | ||
|
||
if hash::verify_password(new_password, &user.classic_auth.unwrap().password_hash) | ||
.await | ||
.is_ok() | ||
{ | ||
error!("Password is the same for user: {}", user_id); | ||
return Err(Error::Auth(Auth::PasswordIsSame)); | ||
} | ||
|
||
let new_password_hash = hash::hash_password(new_password).await?; | ||
let user_id = *user_id; | ||
|
||
conn.interact(move |_| { | ||
let _ = diesel::update(schema::user::table.find(user_id)) | ||
.set(schema::user::updated_at.eq(diesel::dsl::now)); | ||
let _ = diesel::update( | ||
schema::classic_auth::table.filter(schema::classic_auth::user_id.eq(user_id)), | ||
) | ||
.set(schema::classic_auth::password_hash.eq(new_password_hash)); | ||
}) | ||
.await?; | ||
|
||
info!("Password changed successfully for user: {}", user_id); | ||
|
||
Ok(()) | ||
} |