it compiled
This commit is contained in:
@@ -1,29 +1,14 @@
|
|||||||
// src/auth/handlers/register.rs
|
// src/auth/handlers/register.rs
|
||||||
|
|
||||||
use bcrypt::{hash, DEFAULT_COST};
|
use bcrypt::{hash, DEFAULT_COST};
|
||||||
use tonic::{Request, Response, Status};
|
use tonic::{Response, Status};
|
||||||
use common::proto::multieko2::auth::{auth_service_server, RegisterRequest, AuthResponse};
|
use common::proto::multieko2::auth::{RegisterRequest, AuthResponse};
|
||||||
use crate::db::PgPool;
|
use crate::db::PgPool;
|
||||||
use crate::auth::models::AuthError;
|
use crate::auth::models::AuthError;
|
||||||
|
|
||||||
pub struct AuthService {
|
pub async fn register(
|
||||||
pool: PgPool,
|
pool: &PgPool,
|
||||||
}
|
payload: RegisterRequest,
|
||||||
|
|
||||||
impl AuthService {
|
|
||||||
pub fn new(pool: PgPool) -> Self {
|
|
||||||
Self { pool }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tonic::async_trait]
|
|
||||||
impl auth_service_server::AuthService for AuthService {
|
|
||||||
async fn register(
|
|
||||||
&self,
|
|
||||||
request: Request<RegisterRequest>,
|
|
||||||
) -> Result<Response<AuthResponse>, Status> {
|
) -> Result<Response<AuthResponse>, Status> {
|
||||||
let payload = request.into_inner();
|
|
||||||
|
|
||||||
// Validate passwords match
|
// Validate passwords match
|
||||||
if payload.password != payload.password_confirmation {
|
if payload.password != payload.password_confirmation {
|
||||||
return Err(Status::invalid_argument(AuthError::PasswordMismatch.to_string()));
|
return Err(Status::invalid_argument(AuthError::PasswordMismatch.to_string()));
|
||||||
@@ -44,7 +29,7 @@ impl auth_service_server::AuthService for AuthService {
|
|||||||
payload.email,
|
payload.email,
|
||||||
password_hash
|
password_hash
|
||||||
)
|
)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
if e.to_string().contains("duplicate key") {
|
if e.to_string().contains("duplicate key") {
|
||||||
@@ -61,4 +46,3 @@ impl auth_service_server::AuthService for AuthService {
|
|||||||
role: user.role,
|
role: user.role,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use crate::server::services::{
|
|||||||
TableDefinitionService,
|
TableDefinitionService,
|
||||||
TablesDataService,
|
TablesDataService,
|
||||||
TableScriptService,
|
TableScriptService,
|
||||||
|
AuthServiceImpl
|
||||||
};
|
};
|
||||||
use common::proto::multieko2::{
|
use common::proto::multieko2::{
|
||||||
adresar::adresar_server::AdresarServer,
|
adresar::adresar_server::AdresarServer,
|
||||||
@@ -18,11 +19,13 @@ use common::proto::multieko2::{
|
|||||||
table_definition::table_definition_server::TableDefinitionServer,
|
table_definition::table_definition_server::TableDefinitionServer,
|
||||||
tables_data::tables_data_server::TablesDataServer,
|
tables_data::tables_data_server::TablesDataServer,
|
||||||
table_script::table_script_server::TableScriptServer,
|
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<dyn std::error::Error>> {
|
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
// Initialize JWT for authentication
|
||||||
|
crate::auth::logic::jwt::init_jwt()?;
|
||||||
|
|
||||||
let addr = "[::1]:50051".parse()?;
|
let addr = "[::1]:50051".parse()?;
|
||||||
|
|
||||||
let reflection_service = ReflectionBuilder::configure()
|
let reflection_service = ReflectionBuilder::configure()
|
||||||
@@ -33,7 +36,7 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error:
|
|||||||
let table_definition_service = TableDefinitionService { db_pool: db_pool.clone() };
|
let table_definition_service = TableDefinitionService { db_pool: db_pool.clone() };
|
||||||
let tables_data_service = TablesDataService { db_pool: db_pool.clone() };
|
let tables_data_service = TablesDataService { db_pool: db_pool.clone() };
|
||||||
let table_script_service = TableScriptService { db_pool: db_pool.clone() };
|
let table_script_service = TableScriptService { db_pool: db_pool.clone() };
|
||||||
let auth_service = AuthService::new(db_pool.clone()); // Add this line
|
let auth_service = AuthServiceImpl { db_pool: db_pool.clone() };
|
||||||
|
|
||||||
Server::builder()
|
Server::builder()
|
||||||
.add_service(AdresarServer::new(AdresarService { db_pool: db_pool.clone() }))
|
.add_service(AdresarServer::new(AdresarService { db_pool: db_pool.clone() }))
|
||||||
|
|||||||
36
server/src/server/services/auth_service.rs
Normal file
36
server/src/server/services/auth_service.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
// src/server/services/auth_service.rs
|
||||||
|
use tonic::{Request, Response, Status};
|
||||||
|
use common::proto::multieko2::auth::{
|
||||||
|
auth_service_server::AuthService,
|
||||||
|
RegisterRequest, AuthResponse,
|
||||||
|
LoginRequest, LoginResponse
|
||||||
|
};
|
||||||
|
use crate::auth::handlers::{
|
||||||
|
login::login,
|
||||||
|
register::register
|
||||||
|
};
|
||||||
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct AuthServiceImpl {
|
||||||
|
pub db_pool: PgPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tonic::async_trait]
|
||||||
|
impl AuthService for AuthServiceImpl {
|
||||||
|
async fn register(
|
||||||
|
&self,
|
||||||
|
request: Request<RegisterRequest>,
|
||||||
|
) -> Result<Response<AuthResponse>, Status> {
|
||||||
|
let response = register(&self.db_pool, request.into_inner()).await?;
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn login(
|
||||||
|
&self,
|
||||||
|
request: Request<LoginRequest>,
|
||||||
|
) -> Result<Response<LoginResponse>, Status> {
|
||||||
|
let response = login(&self.db_pool, request.into_inner()).await?;
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ pub mod uctovnictvo_service;
|
|||||||
pub mod table_definition_service;
|
pub mod table_definition_service;
|
||||||
pub mod tables_data_service;
|
pub mod tables_data_service;
|
||||||
pub mod table_script_service;
|
pub mod table_script_service;
|
||||||
|
pub mod auth_service;
|
||||||
|
|
||||||
pub use adresar_service::AdresarService;
|
pub use adresar_service::AdresarService;
|
||||||
pub use table_structure_service::TableStructureHandler;
|
pub use table_structure_service::TableStructureHandler;
|
||||||
@@ -13,3 +14,4 @@ pub use uctovnictvo_service::UctovnictvoService;
|
|||||||
pub use table_definition_service::TableDefinitionService;
|
pub use table_definition_service::TableDefinitionService;
|
||||||
pub use tables_data_service::TablesDataService;
|
pub use tables_data_service::TablesDataService;
|
||||||
pub use table_script_service::TableScriptService;
|
pub use table_script_service::TableScriptService;
|
||||||
|
pub use auth_service::AuthServiceImpl;
|
||||||
|
|||||||
Reference in New Issue
Block a user