2 use actix_web::web::Data;
4 use lemmy_api_common::{
5 person::{ChangePassword, LoginResponse},
6 utils::{get_local_user_view_from_jwt, password_length_check},
8 use lemmy_db_schema::source::local_user::LocalUser;
9 use lemmy_utils::{claims::Claims, error::LemmyError, ConnectionId};
10 use lemmy_websocket::LemmyContext;
12 #[async_trait::async_trait(?Send)]
13 impl Perform for ChangePassword {
14 type Response = LoginResponse;
16 #[tracing::instrument(skip(self, context, _websocket_id))]
19 context: &Data<LemmyContext>,
20 _websocket_id: Option<ConnectionId>,
21 ) -> Result<LoginResponse, LemmyError> {
22 let data: &ChangePassword = self;
24 get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?;
26 password_length_check(&data.new_password)?;
28 // Make sure passwords match
29 if data.new_password != data.new_password_verify {
30 return Err(LemmyError::from_message("passwords_dont_match"));
33 // Check the old password
34 let valid: bool = verify(
36 &local_user_view.local_user.password_encrypted,
40 return Err(LemmyError::from_message("password_incorrect"));
43 let local_user_id = local_user_view.local_user.id;
44 let new_password = data.new_password.to_owned();
45 let updated_local_user =
46 LocalUser::update_password(context.pool(), local_user_id, &new_password).await?;
52 updated_local_user.id.0,
53 &context.secret().jwt_secret,
54 &context.settings().hostname,
58 verify_email_sent: false,
59 registration_created: false,