Skip to content

Commit

Permalink
Merge pull request #50 from traP-jp/feat/#18-post-reset-password
Browse files Browse the repository at this point in the history
POST /reset-password を実装
  • Loading branch information
kenken714 authored Oct 30, 2024
2 parents 11b435d + 92a89f5 commit f71c610
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub fn make_router(app_state: Repository) -> Router {
.route(
"/reset-password/request",
post(authentication::reset_password_request),
);
)
.route("/reset-password", post(authentication::reset_password));

let users_router = Router::new()
.route("/me", get(users::get_me).put(users::put_me))
Expand Down
38 changes: 38 additions & 0 deletions src/handler/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,41 @@ https://link/{jwt}"

Ok(StatusCode::CREATED)
}

#[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<Repository>,
Json(body): Json<ResetPassword>,
) -> Result<StatusCode, StatusCode> {
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)
}

0 comments on commit f71c610

Please sign in to comment.