it compiled
This commit is contained in:
@@ -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<Response<AuthResponse>, 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<RegisterRequest>,
|
||||
) -> Result<Response<AuthResponse>, 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,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
// 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<dyn std::error:
|
||||
let table_definition_service = TableDefinitionService { 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 auth_service = AuthService::new(db_pool.clone()); // Add this line
|
||||
let auth_service = AuthServiceImpl { db_pool: db_pool.clone() };
|
||||
|
||||
Server::builder()
|
||||
.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 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;
|
||||
|
||||
Reference in New Issue
Block a user