diff --git a/visions/server/src/handlers/mod.rs b/visions/server/src/handlers/mod.rs index c742b9d..740f5ad 100644 --- a/visions/server/src/handlers/mod.rs +++ b/visions/server/src/handlers/mod.rs @@ -74,6 +74,22 @@ pub async fn check_password( } } +pub async fn authenticated( + core: Core, + headers: HeaderMap, + f: F, +) -> (StatusCode, Json>) +where + F: FnOnce(User) -> (StatusCode, Json>), +{ + match check_session(&core, headers).await { + ResultExt::Ok(Some(user)) => f(user), + ResultExt::Ok(None) => (StatusCode::UNAUTHORIZED, Json(None)), + ResultExt::Err(_err) => (StatusCode::BAD_REQUEST, Json(None)), + ResultExt::Fatal(err) => panic!("{}", err), + } +} + #[derive(Deserialize, Serialize)] #[typeshare] pub struct UserProfile { @@ -82,14 +98,16 @@ pub struct UserProfile { } pub async fn get_user(core: Core, headers: HeaderMap) -> (StatusCode, Json>) { - match check_session(&core, headers).await { - ResultExt::Ok(Some(user)) => { - (StatusCode::OK, Json(Some(UserProfile{userid: UserId::from(user.id), username: user.name} ))) - } - ResultExt::Ok(None) => (StatusCode::UNAUTHORIZED, Json(None)), - ResultExt::Err(_err) => (StatusCode::BAD_REQUEST, Json(None)), - ResultExt::Fatal(err) => panic!("{}", err), - } + authenticated(core.clone(), headers, |user| { + ( + StatusCode::OK, + Json(Some(UserProfile { + userid: UserId::from(user.id), + username: user.name, + })), + ) + }) + .await } /*