Refactor the API, then give the user a landing page that shows their profile #286

Merged
savanni merged 23 commits from visions-refactor-api into main 2025-01-03 22:00:02 +00:00
Showing only changes of commit 82e41d711b - Show all commits

View File

@ -74,6 +74,22 @@ pub async fn check_password(
}
}
pub async fn authenticated<F, A>(
core: Core,
headers: HeaderMap,
f: F,
) -> (StatusCode, Json<Option<A>>)
where
F: FnOnce(User) -> (StatusCode, Json<Option<A>>),
{
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<Option<UserProfile>>) {
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
}
/*