From d1102dcf8b333d7cc51a454998cebe0a60c5e4b0 Mon Sep 17 00:00:00 2001 From: Fabrice Bellamy <12b@distrilab.fr> Date: Mon, 21 Oct 2024 17:01:55 +0200 Subject: [PATCH] add code to require authentication for 2 controllers functions (2 different syntax) --- nixin_farm_ssr/src/controllers/server.rs | 6 ++++++ nixin_farm_ssr/src/controllers/service.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/nixin_farm_ssr/src/controllers/server.rs b/nixin_farm_ssr/src/controllers/server.rs index 1de9dda..97a7c80 100644 --- a/nixin_farm_ssr/src/controllers/server.rs +++ b/nixin_farm_ssr/src/controllers/server.rs @@ -10,6 +10,7 @@ use crate::{ models::_entities::servers::{ActiveModel, Column, Entity, Model}, views, }; +use crate::models::users; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Params { @@ -31,9 +32,14 @@ async fn load_item(ctx: &AppContext, id: i32) -> Result { #[debug_handler] pub async fn list( + auth: auth::JWT, ViewEngine(v): ViewEngine, State(ctx): State, ) -> Result { + // we only want to make sure user exists, so we name the variable + // with a `_` in front to remove the warning about unused variable + let _current_user = users::Model::find_by_pid(&ctx.db, &auth.claims.pid).await?; + let item = Entity::find() .order_by(Column::Id, Order::Desc) .all(&ctx.db) diff --git a/nixin_farm_ssr/src/controllers/service.rs b/nixin_farm_ssr/src/controllers/service.rs index 0684574..2b74353 100644 --- a/nixin_farm_ssr/src/controllers/service.rs +++ b/nixin_farm_ssr/src/controllers/service.rs @@ -10,6 +10,7 @@ use crate::{ models::_entities::services::{ActiveModel, Column, Entity, Model}, views, }; +use crate::models::users; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Params { @@ -33,6 +34,7 @@ async fn load_item(ctx: &AppContext, id: i32) -> Result { #[debug_handler] pub async fn list( + _user: auth::ApiToken, ViewEngine(v): ViewEngine, State(ctx): State, ) -> Result {