]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/change_password_after_reset.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / api / src / local_user / change_password_after_reset.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   person::{LoginResponse, PasswordChangeAfterReset},
6   utils::password_length_check,
7 };
8 use lemmy_db_schema::source::{
9   local_user::LocalUser,
10   password_reset_request::PasswordResetRequest,
11 };
12 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
13
14 #[async_trait::async_trait(?Send)]
15 impl Perform for PasswordChangeAfterReset {
16   type Response = LoginResponse;
17
18   #[tracing::instrument(skip(self, context))]
19   async fn perform(&self, context: &Data<LemmyContext>) -> Result<LoginResponse, LemmyError> {
20     let data: &PasswordChangeAfterReset = self;
21
22     // Fetch the user_id from the token
23     let token = data.token.clone();
24     let local_user_id = PasswordResetRequest::read_from_token(&mut context.pool(), &token)
25       .await
26       .map(|p| p.local_user_id)?;
27
28     password_length_check(&data.password)?;
29
30     // Make sure passwords match
31     if data.password != data.password_verify {
32       return Err(LemmyErrorType::PasswordsDoNotMatch)?;
33     }
34
35     // Update the user with the new password
36     let password = data.password.clone();
37     LocalUser::update_password(&mut context.pool(), local_user_id, &password)
38       .await
39       .with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
40
41     Ok(LoginResponse {
42       jwt: None,
43       verify_email_sent: false,
44       registration_created: false,
45     })
46   }
47 }