fight unuseds, sessions endpoint

This commit is contained in:
2026-02-24 01:42:19 +01:00
parent ee7ed48144
commit 1e7866a293
11 changed files with 59 additions and 13 deletions

View File

@@ -4,18 +4,19 @@ use axum::{
routing::get,
};
use crate::{
api::users::{get_by_id, get_me},
users::{UserError, auth::AuthError, sessions::SessionError},
};
use crate::users::{UserError, auth::AuthError, sessions::SessionError};
mod sessions;
mod users;
// TODO: PERMISSIONS FOR ENDPOINTS & ACTIONS
pub fn api_router() -> Router {
Router::new()
.route("/api/live", get(async || "Mnemosyne lives"))
.route("/api/users/me", get(get_me))
.route("/api/users/{id}", get(get_by_id))
.route("/api/users/me", get(users::get_me))
.route("/api/users/{id}", get(users::get_by_id))
.route("/api/users/@{handle}", get(users::get_by_handle))
.route("/api/sessions/{id}", get(sessions::get_by_id))
}
pub struct CompositeError(Response);

24
src/api/sessions.rs Normal file
View File

@@ -0,0 +1,24 @@
use axum::{
Json,
extract::Path,
http::HeaderMap,
response::{IntoResponse, Response},
};
use uuid::Uuid;
use crate::{
api::CompositeError,
users::{
User,
auth::{UserAuthRequired, UserAuthenticate},
sessions::Session,
},
};
pub async fn get_by_id(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(Session::get_by_id(id)?).into_response())
}

View File

@@ -1,7 +1,7 @@
use axum::{
Json,
extract::Path,
http::{HeaderMap, StatusCode},
http::HeaderMap,
response::{IntoResponse, Response},
};
use uuid::Uuid;
@@ -11,14 +11,26 @@ use crate::{
users::{
User,
auth::{UserAuthRequired, UserAuthenticate},
handle::UserHandle,
},
};
pub async fn get_me(h: HeaderMap) -> Result<Response, CompositeError> {
Ok(Json(User::authenticate(&h)?.required()?).into_response())
pub async fn get_me(headers: HeaderMap) -> Result<Response, CompositeError> {
Ok(Json(User::authenticate(&headers)?.required()?).into_response())
}
pub async fn get_by_id(Path(id): Path<Uuid>, h: HeaderMap) -> Result<Response, CompositeError> {
User::authenticate(&h)?.required()?;
pub async fn get_by_id(
Path(id): Path<Uuid>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(User::get_by_id(id)?).into_response())
}
pub async fn get_by_handle(
Path(handle): Path<UserHandle>,
headers: HeaderMap,
) -> Result<Response, CompositeError> {
User::authenticate(&headers)?.required()?;
Ok(Json(User::get_by_handle(handle)?).into_response())
}