summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-08-23 18:28:14 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-08-23 18:28:14 +0200
commitf66a763cfc275c87407b5a9559628802f9646b95 (patch)
tree7d34ddf2cb5ac7254a9b205d6591e174dea8b9c9 /server
parentb49ca4d0ded65179a10c9924114381d9026c0e26 (diff)
Add authorization on users
Diffstat (limited to 'server')
-rw-r--r--server/src/routes/user.rs14
1 files changed, 10 insertions, 4 deletions
diff --git a/server/src/routes/user.rs b/server/src/routes/user.rs
index 4103ccb..3ca0e7b 100644
--- a/server/src/routes/user.rs
+++ b/server/src/routes/user.rs
@@ -1,5 +1,8 @@
use crate::errors::AppError;
-use crate::models::user::{User, UserCreate, UserList};
+use crate::models::{
+ auth::Claims,
+ user::{User, UserCreate, UserList},
+};
use axum::{extract::Path, routing::get, Json, Router};
pub fn create_route() -> Router {
@@ -8,20 +11,23 @@ pub fn create_route() -> Router {
.route("/:id", get(get_user))
}
-async fn list_users() -> Result<Json<Vec<UserList>>, AppError> {
+async fn list_users(_: Claims) -> Result<Json<Vec<UserList>>, AppError> {
let users = User::list().await?;
Ok(Json(users))
}
-async fn create_user(Json(payload): Json<UserCreate>) -> Result<Json<UserList>, AppError> {
+async fn create_user(
+ Json(payload): Json<UserCreate>,
+ _: Claims,
+) -> Result<Json<UserList>, AppError> {
let user = User::new(payload.email, payload.password);
let user_new = User::create(user).await?;
Ok(Json(user_new))
}
-async fn get_user(Path(user_id): Path<i32>) -> Result<Json<UserList>, AppError> {
+async fn get_user(Path(user_id): Path<i32>, _: Claims) -> Result<Json<UserList>, AppError> {
match User::find_by_id(user_id).await {
Ok(user) => Ok(Json(user)),
Err(_) => Err(AppError::NotFound),