Skip to content

Commit

Permalink
Merge pull request #44 from traP-jp/refactor/put-me-password
Browse files Browse the repository at this point in the history
put_me_password で  get_user_id_by_session_id を使うように変更
  • Loading branch information
kenken714 authored Oct 29, 2024
2 parents 5144703 + dbd005f commit 2a2c730
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
17 changes: 4 additions & 13 deletions src/handler/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,16 @@ pub async fn put_me_password(
body.validate().map_err(|_| StatusCode::BAD_REQUEST)?;
let session_id = cookie.get("session_id").ok_or(StatusCode::UNAUTHORIZED)?;

let display_id = state
.get_display_id_by_session_id(session_id)
let id = state
.get_user_id_by_session_id(session_id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::UNAUTHORIZED)?;

let user = state
.get_user_by_display_id(display_id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(StatusCode::INTERNAL_SERVER_ERROR)?;

match state
.verify_user_password(user.id, &body.old_password)
.await
{
match state.verify_user_password(id, &body.old_password).await {
Ok(true) => {
state
.update_user_password(user.id, &body.new_password)
.update_user_password(id, &body.new_password)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::NO_CONTENT)
Expand Down
13 changes: 10 additions & 3 deletions src/repository/users_session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::Repository;
use super::{users::UserId, Repository};
use anyhow::Context;
use async_session::{Session, SessionStore};

Expand Down Expand Up @@ -38,13 +38,20 @@ impl Repository {
Ok(Some(()))
}

pub async fn get_user_id_by_session_id(&self, session_id: &str) -> anyhow::Result<Option<i64>> {
pub async fn get_user_id_by_session_id(
&self,
session_id: &str,
) -> anyhow::Result<Option<UserId>> {
let session = self
.session_store
.load_session(session_id.to_string())
.await?;

Ok(session.and_then(|s| s.get("user_id")))
let user_id = session
.and_then(|s| s.get("user_id"))
.map(|id: uuid::Uuid| UserId::new(id));

Ok(user_id)
}

pub async fn get_display_id_by_session_id(
Expand Down

0 comments on commit 2a2c730

Please sign in to comment.