From 11db0b0244519e9e899a1e54ceed5032ba695311 Mon Sep 17 00:00:00 2001 From: kenken714 Date: Tue, 29 Oct 2024 17:32:03 +0900 Subject: [PATCH 1/2] feat: POST /reset-password --- src/handler.rs | 3 ++- src/handler/authentication.rs | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/handler.rs b/src/handler.rs index d177ab0..a1a4a26 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -13,7 +13,8 @@ pub fn make_router(app_state: Repository) -> Router { .route("/signup/request", post(authentication::sign_up_request)) .route("/signup", post(authentication::sign_up)) .route("/login", post(authentication::login)) - .route("/logout", post(authentication::logout)); + .route("/logout", post(authentication::logout)) + .route("/reset-password", post(authentication::reset_password)); let users_router = Router::new() .route("/me", get(users::get_me).put(users::put_me)) diff --git a/src/handler/authentication.rs b/src/handler/authentication.rs index c808144..62af636 100644 --- a/src/handler/authentication.rs +++ b/src/handler/authentication.rs @@ -164,3 +164,41 @@ pub async fn logout( Ok((StatusCode::NO_CONTENT, headers)) } + +#[derive(Deserialize)] +pub struct ResetPassword { + password: String, + token: String, +} + +impl Validator for ResetPassword { + fn validate(&self) -> anyhow::Result<()> { + RuleType::Password.validate(&self.password)?; + Ok(()) + } +} + +pub async fn reset_password( + State(state): State, + Json(body): Json, +) -> Result { + body.validate().map_err(|_| StatusCode::BAD_REQUEST)?; + + let email = state + .get_email_by_email_jwt(&body.token) + .await + .map_err(|_| StatusCode::UNAUTHORIZED)?; + + let user = state + .get_user_by_email(&email) + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? + .ok_or(StatusCode::UNAUTHORIZED)?; + + state + .update_user_password(user.id, &body.password) + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + Ok(StatusCode::NO_CONTENT) +} From 92a89f583422f879acfbbd833a4dbef49150f9f2 Mon Sep 17 00:00:00 2001 From: kenken714 Date: Wed, 30 Oct 2024 13:43:33 +0900 Subject: [PATCH 2/2] fix: missing row --- src/handler/authentication.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/handler/authentication.rs b/src/handler/authentication.rs index 509f952..d5ca8ee 100644 --- a/src/handler/authentication.rs +++ b/src/handler/authentication.rs @@ -165,6 +165,7 @@ pub async fn logout( Ok((StatusCode::NO_CONTENT, headers)) } +#[derive(Deserialize)] pub struct ResetPasswordRequest { email: String, } @@ -201,6 +202,7 @@ https://link/{jwt}" Ok(StatusCode::CREATED) } +#[derive(Deserialize)] pub struct ResetPassword { password: String, token: String,