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,
|
2023-10-16 16:36:53 +00:00
|
|
|
person::PasswordChangeAfterReset,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::password_length_check,
|
2023-10-16 16:36:53 +00:00
|
|
|
SuccessResponse,
|
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
|
|
|
};
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
|
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>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Json<SuccessResponse>> {
|
2023-09-05 09:33:46 +00:00
|
|
|
// Fetch the user_id from the token
|
|
|
|
let token = data.token.clone();
|
|
|
|
let local_user_id = PasswordResetRequest::read_from_token(&mut context.pool(), &token)
|
2024-04-16 12:48:15 +00:00
|
|
|
.await?
|
|
|
|
.ok_or(LemmyErrorType::TokenNotFound)?
|
|
|
|
.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-10-16 16:36:53 +00:00
|
|
|
Ok(Json(SuccessResponse::default()))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|