From f40654d2c4dc3099029fbb655e9d4b21b432e1a6 Mon Sep 17 00:00:00 2001 From: filipriec Date: Tue, 25 Mar 2025 10:28:29 +0100 Subject: [PATCH] it compiled --- server/src/auth/handlers/register.rs | 90 +++++++++------------- server/src/server/run.rs | 9 ++- server/src/server/services/auth_service.rs | 36 +++++++++ server/src/server/services/mod.rs | 2 + 4 files changed, 81 insertions(+), 56 deletions(-) create mode 100644 server/src/server/services/auth_service.rs diff --git a/server/src/auth/handlers/register.rs b/server/src/auth/handlers/register.rs index 9d0d63d..d9dc761 100644 --- a/server/src/auth/handlers/register.rs +++ b/server/src/auth/handlers/register.rs @@ -1,64 +1,48 @@ // src/auth/handlers/register.rs - use bcrypt::{hash, DEFAULT_COST}; -use tonic::{Request, Response, Status}; -use common::proto::multieko2::auth::{auth_service_server, RegisterRequest, AuthResponse}; +use tonic::{Response, Status}; +use common::proto::multieko2::auth::{RegisterRequest, AuthResponse}; use crate::db::PgPool; use crate::auth::models::AuthError; -pub struct AuthService { - pool: PgPool, -} - -impl AuthService { - pub fn new(pool: PgPool) -> Self { - Self { pool } +pub async fn register( + pool: &PgPool, + payload: RegisterRequest, +) -> Result, Status> { + // Validate passwords match + if payload.password != payload.password_confirmation { + return Err(Status::invalid_argument(AuthError::PasswordMismatch.to_string())); } -} -#[tonic::async_trait] -impl auth_service_server::AuthService for AuthService { - async fn register( - &self, - request: Request, - ) -> Result, Status> { - let payload = request.into_inner(); + // Hash password + let password_hash = hash(payload.password, DEFAULT_COST) + .map_err(|e| Status::internal(AuthError::HashingError(e.to_string()).to_string()))?; - // Validate passwords match - if payload.password != payload.password_confirmation { - return Err(Status::invalid_argument(AuthError::PasswordMismatch.to_string())); + // Insert user + let user = sqlx::query!( + r#" + INSERT INTO users (username, email, password_hash, role) + VALUES ($1, $2, $3, 'accountant') + RETURNING id, username, email, role + "#, + payload.username, + payload.email, + password_hash + ) + .fetch_one(pool) + .await + .map_err(|e| { + if e.to_string().contains("duplicate key") { + Status::already_exists(AuthError::UserExists.to_string()) + } else { + Status::internal(AuthError::DatabaseError(e.to_string()).to_string()) } + })?; - // Hash password - let password_hash = hash(payload.password, DEFAULT_COST) - .map_err(|e| Status::internal(AuthError::HashingError(e.to_string()).to_string()))?; - - // Insert user - let user = sqlx::query!( - r#" - INSERT INTO users (username, email, password_hash, role) - VALUES ($1, $2, $3, 'accountant') - RETURNING id, username, email, role - "#, - payload.username, - payload.email, - password_hash - ) - .fetch_one(&self.pool) - .await - .map_err(|e| { - if e.to_string().contains("duplicate key") { - Status::already_exists(AuthError::UserExists.to_string()) - } else { - Status::internal(AuthError::DatabaseError(e.to_string()).to_string()) - } - })?; - - Ok(Response::new(AuthResponse { - id: user.id.to_string(), - username: user.username, - email: user.email.unwrap_or_default(), - role: user.role, - })) - } + Ok(Response::new(AuthResponse { + id: user.id.to_string(), + username: user.username, + email: user.email.unwrap_or_default(), + role: user.role, + })) } diff --git a/server/src/server/run.rs b/server/src/server/run.rs index dadb687..38ddc62 100644 --- a/server/src/server/run.rs +++ b/server/src/server/run.rs @@ -10,6 +10,7 @@ use crate::server::services::{ TableDefinitionService, TablesDataService, TableScriptService, + AuthServiceImpl }; use common::proto::multieko2::{ adresar::adresar_server::AdresarServer, @@ -18,11 +19,13 @@ use common::proto::multieko2::{ table_definition::table_definition_server::TableDefinitionServer, tables_data::tables_data_server::TablesDataServer, table_script::table_script_server::TableScriptServer, - auth::auth_service_server::AuthServiceServer // Add this import + auth::auth_service_server::AuthServiceServer }; -use crate::auth::handlers::AuthService; // Add this import pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box> { + // Initialize JWT for authentication + crate::auth::logic::jwt::init_jwt()?; + let addr = "[::1]:50051".parse()?; let reflection_service = ReflectionBuilder::configure() @@ -33,7 +36,7 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box, + ) -> Result, Status> { + let response = register(&self.db_pool, request.into_inner()).await?; + Ok(response) + } + + async fn login( + &self, + request: Request, + ) -> Result, Status> { + let response = login(&self.db_pool, request.into_inner()).await?; + Ok(response) + } +} diff --git a/server/src/server/services/mod.rs b/server/src/server/services/mod.rs index 27c956a..80c50ac 100644 --- a/server/src/server/services/mod.rs +++ b/server/src/server/services/mod.rs @@ -6,6 +6,7 @@ pub mod uctovnictvo_service; pub mod table_definition_service; pub mod tables_data_service; pub mod table_script_service; +pub mod auth_service; pub use adresar_service::AdresarService; pub use table_structure_service::TableStructureHandler; @@ -13,3 +14,4 @@ pub use uctovnictvo_service::UctovnictvoService; pub use table_definition_service::TableDefinitionService; pub use tables_data_service::TablesDataService; pub use table_script_service::TableScriptService; +pub use auth_service::AuthServiceImpl;