diff --git a/src/routes/handlers/auth_handlers.rs b/src/routes/handlers/auth_handlers.rs index f69b96a..f428589 100644 --- a/src/routes/handlers/auth_handlers.rs +++ b/src/routes/handlers/auth_handlers.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use crate::utils::{ api_response::{self, ApiResponse}, app_state::{self, AppState}, + auth::get_user_from_email, global_variables::DYNAMO_DB_TABLE_NAME, jwt::{add_to_blacklist, encode_jwt}, models::User, @@ -30,7 +31,16 @@ pub async fn register( app_state: web::Data, request: web::Json, ) -> Result { + let result = get_user_from_email(&app_state.dynamo_client, request.email.clone()) + .await + .map_err(|err| ApiResponse::new(500, err.to_string()))?; + + if let Some(_) = result.items.and_then(|items| items.first().cloned()) { + return Err(ApiResponse::new(409, "User already exists".to_string())); + } + let mut item = HashMap::new(); + item.insert( "id".to_string(), AttributeValue::S(format!("USER#{}", uuid::Uuid::new_v4())), @@ -68,24 +78,9 @@ pub async fn login( request: web::Json, ) -> Result { println!("first"); - let table_name = DYNAMO_DB_TABLE_NAME.clone(); - - let result = app_state - .dynamo_client - .query() - .table_name(table_name) - .index_name("EmailIndex") // Assuming you've created a GSI named "EmailIndex" - .key_condition_expression("email = :email") - .expression_attribute_values(":email", AttributeValue::S(request.email.clone())) - .select(aws_sdk_dynamodb::types::Select::AllAttributes) - .send() + let result = get_user_from_email(&app_state.dynamo_client, request.email.clone()) .await - .map_err(|err| { - ApiResponse::new( - 500, - format!("DynamoDB query failed: {}. Error details: {:?}", err, err), - ) - })?; + .map_err(|err| ApiResponse::new(409, err.to_string()))?; let user = result .items diff --git a/src/utils/auth.rs b/src/utils/auth.rs new file mode 100644 index 0000000..4346c6c --- /dev/null +++ b/src/utils/auth.rs @@ -0,0 +1,23 @@ +use anyhow::Result; +use std::sync::Arc; + +use aws_sdk_dynamodb::{operation::query::QueryOutput, types::AttributeValue, Client}; + +use super::{api_response::ApiResponse, global_variables::DYNAMO_DB_TABLE_NAME}; + +pub(crate) async fn get_user_from_email( + dynamo_client: &Arc, + email: String, +) -> Result { + let table_name = DYNAMO_DB_TABLE_NAME.clone(); + dynamo_client + .query() + .table_name(table_name) + .index_name("EmailIndex") // Assuming you've created a GSI named "EmailIndex" + .key_condition_expression("email = :email") + .expression_attribute_values(":email", AttributeValue::S(email)) + .select(aws_sdk_dynamodb::types::Select::AllAttributes) + .send() + .await + .map_err(anyhow::Error::from) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 98c6f22..c173107 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,6 @@ pub mod api_response; pub mod app_state; +pub mod auth; pub mod environment_variables; pub mod global_variables; pub mod jwt;