2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 person::{ChangePassword, LoginResponse},
7 utils::{local_user_view_from_jwt, password_length_check},
9 use lemmy_db_schema::source::local_user::LocalUser;
10 use lemmy_utils::{claims::Claims, error::LemmyError};
12 #[async_trait::async_trait(?Send)]
13 impl Perform for ChangePassword {
14 type Response = LoginResponse;
16 #[tracing::instrument(skip(self, context))]
17 async fn perform(&self, context: &Data<LemmyContext>) -> Result<LoginResponse, LemmyError> {
18 let data: &ChangePassword = self;
19 let local_user_view = local_user_view_from_jwt(data.auth.as_ref(), context).await?;
21 password_length_check(&data.new_password)?;
23 // Make sure passwords match
24 if data.new_password != data.new_password_verify {
25 return Err(LemmyError::from_message("passwords_dont_match"));
28 // Check the old password
29 let valid: bool = verify(
31 &local_user_view.local_user.password_encrypted,
35 return Err(LemmyError::from_message("password_incorrect"));
38 let local_user_id = local_user_view.local_user.id;
39 let new_password = data.new_password.clone();
40 let updated_local_user =
41 LocalUser::update_password(context.pool(), local_user_id, &new_password).await?;
47 updated_local_user.id.0,
48 &context.secret().jwt_secret,
49 &context.settings().hostname,
53 verify_email_sent: false,
54 registration_created: false,