2022-04-13 18:12:25 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-04-13 18:12:25 +00:00
|
|
|
person::{LoginResponse, PasswordChangeAfterReset},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::password_length_check,
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
2023-07-25 17:33:02 +00:00
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
local_user::LocalUser,
|
|
|
|
password_reset_request::PasswordResetRequest,
|
2023-07-10 14:50:07 +00:00
|
|
|
};
|
2023-07-25 17:33:02 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for PasswordChangeAfterReset {
|
|
|
|
type Response = LoginResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(self, context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<LoginResponse, LemmyError> {
|
2022-04-13 18:12:25 +00:00
|
|
|
let data: &PasswordChangeAfterReset = self;
|
|
|
|
|
|
|
|
// Fetch the user_id from the token
|
|
|
|
let token = data.token.clone();
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_user_id = PasswordResetRequest::read_from_token(&mut context.pool(), &token)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
|
|
|
.map(|p| p.local_user_id)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
password_length_check(&data.password)?;
|
|
|
|
|
|
|
|
// Make sure passwords match
|
|
|
|
if data.password != data.password_verify {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::PasswordsDoNotMatch)?
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the user with the new password
|
|
|
|
let password = data.password.clone();
|
2023-07-25 17:33:02 +00:00
|
|
|
LocalUser::update_password(&mut context.pool(), local_user_id, &password)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
|
2022-12-01 21:36:03 +00:00
|
|
|
|
2022-04-13 18:12:25 +00:00
|
|
|
Ok(LoginResponse {
|
2023-07-25 17:33:02 +00:00
|
|
|
jwt: None,
|
2022-04-13 18:12:25 +00:00
|
|
|
verify_email_sent: false,
|
|
|
|
registration_created: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|