From f66a763cfc275c87407b5a9559628802f9646b95 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 23 Aug 2022 18:28:14 +0200 Subject: Add authorization on users --- server/src/routes/user.rs | 14 ++++++++++---- 1 file 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>, AppError> { +async fn list_users(_: Claims) -> Result>, AppError> { let users = User::list().await?; Ok(Json(users)) } -async fn create_user(Json(payload): Json) -> Result, AppError> { +async fn create_user( + Json(payload): Json, + _: Claims, +) -> Result, 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) -> Result, AppError> { +async fn get_user(Path(user_id): Path, _: Claims) -> Result, AppError> { match User::find_by_id(user_id).await { Ok(user) => Ok(Json(user)), Err(_) => Err(AppError::NotFound), -- cgit v1.2.3-18-g5258