CompositeError, UserAuthRequired, /users/self & users/:id, misc
This commit is contained in:
@@ -1,5 +1,39 @@
|
||||
use axum::{Router, routing::get};
|
||||
use axum::{
|
||||
Router,
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
api::users::{get_by_id, get_me},
|
||||
users::{UserError, auth::AuthError, sessions::SessionError},
|
||||
};
|
||||
|
||||
mod users;
|
||||
|
||||
pub fn api_router() -> Router {
|
||||
Router::new().route("/api/live", get(async || "Mnemosyne lives"))
|
||||
Router::new()
|
||||
.route("/api/live", get(async || "Mnemosyne lives"))
|
||||
.route("/api/users/me", get(get_me))
|
||||
.route("/api/users/{id}", get(get_by_id))
|
||||
}
|
||||
|
||||
pub struct CompositeError(Response);
|
||||
impl IntoResponse for CompositeError {
|
||||
fn into_response(self) -> Response {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! composite_from {
|
||||
($($t:ty),+ $(,)?) => {
|
||||
$(
|
||||
impl From<$t> for CompositeError {
|
||||
fn from(e: $t) -> Self {
|
||||
CompositeError(e.into_response())
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
composite_from!(AuthError, UserError, SessionError);
|
||||
|
||||
24
src/api/users.rs
Normal file
24
src/api/users.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use axum::{
|
||||
Json,
|
||||
extract::Path,
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::CompositeError,
|
||||
users::{
|
||||
User,
|
||||
auth::{UserAuthRequired, UserAuthenticate},
|
||||
},
|
||||
};
|
||||
|
||||
pub async fn get_me(h: HeaderMap) -> Result<Response, CompositeError> {
|
||||
Ok(Json(User::authenticate(&h)?.required()?).into_response())
|
||||
}
|
||||
|
||||
pub async fn get_by_id(Path(id): Path<Uuid>, h: HeaderMap) -> Result<Response, CompositeError> {
|
||||
User::authenticate(&h)?.required()?;
|
||||
Ok(Json(User::get_by_id(id)?).into_response())
|
||||
}
|
||||
Reference in New Issue
Block a user