2023-09-05 09:33:46 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2022-04-13 18:12:25 +00:00
|
|
|
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,
|
2023-10-09 10:46:12 +00:00
|
|
|
login_token::LoginToken,
|
2023-07-25 17:33:02 +00:00
|
|
|
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
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn change_password_after_reset(
|
|
|
|
data: Json<PasswordChangeAfterReset>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<LoginResponse>, LemmyError> {
|
|
|
|
// Fetch the user_id from the token
|
|
|
|
let token = data.token.clone();
|
|
|
|
let local_user_id = PasswordResetRequest::read_from_token(&mut context.pool(), &token)
|
|
|
|
.await
|
|
|
|
.map(|p| p.local_user_id)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
password_length_check(&data.password)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Make sure passwords match
|
|
|
|
if data.password != data.password_verify {
|
|
|
|
Err(LemmyErrorType::PasswordsDoNotMatch)?
|
|
|
|
}
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
// Update the user with the new password
|
|
|
|
let password = data.password.clone();
|
|
|
|
LocalUser::update_password(&mut context.pool(), local_user_id, &password)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
|
2022-12-01 21:36:03 +00:00
|
|
|
|
2023-10-09 10:46:12 +00:00
|
|
|
LoginToken::invalidate_all(&mut context.pool(), local_user_id).await?;
|
|
|
|
|
2023-09-05 09:33:46 +00:00
|
|
|
Ok(Json(LoginResponse {
|
|
|
|
jwt: None,
|
|
|
|
verify_email_sent: false,
|
|
|
|
registration_created: false,
|
|
|
|
}))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|