]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/reset_password.rs
0f896f477c287d27f73d96aac38fde4b0438fec8
[lemmy.git] / crates / api / src / local_user / reset_password.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   context::LemmyContext,
5   person::{PasswordReset, PasswordResetResponse},
6   utils::send_password_reset_email,
7 };
8 use lemmy_db_schema::source::password_reset_request::PasswordResetRequest;
9 use lemmy_db_views::structs::LocalUserView;
10 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
11
12 #[async_trait::async_trait(?Send)]
13 impl Perform for PasswordReset {
14   type Response = PasswordResetResponse;
15
16   #[tracing::instrument(skip(self, context))]
17   async fn perform(
18     &self,
19     context: &Data<LemmyContext>,
20   ) -> Result<PasswordResetResponse, LemmyError> {
21     let data: &PasswordReset = self;
22
23     // Fetch that email
24     let email = data.email.to_lowercase();
25     let local_user_view = LocalUserView::find_by_email(context.pool(), &email)
26       .await
27       .with_lemmy_type(LemmyErrorType::IncorrectLogin)?;
28
29     // Check for too many attempts (to limit potential abuse)
30     let recent_resets_count = PasswordResetRequest::get_recent_password_resets_count(
31       context.pool(),
32       local_user_view.local_user.id,
33     )
34     .await?;
35     if recent_resets_count >= 3 {
36       return Err(LemmyErrorType::PasswordResetLimitReached)?;
37     }
38
39     // Email the pure token to the user.
40     send_password_reset_email(&local_user_view, context.pool(), context.settings()).await?;
41     Ok(PasswordResetResponse {})
42   }
43 }