Skip to content

Commit

Permalink
add detection for duplicated registration with same email
Browse files Browse the repository at this point in the history
  • Loading branch information
vxcall committed Sep 2, 2024
1 parent 8fa0961 commit e4cbf7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/routes/handlers/auth_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -30,7 +31,16 @@ pub async fn register(
app_state: web::Data<AppState>,
request: web::Json<RegisterRequest>,
) -> Result<ApiResponse, ApiResponse> {
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())),
Expand Down Expand Up @@ -68,24 +78,9 @@ pub async fn login(
request: web::Json<LoginRequest>,
) -> Result<ApiResponse, ApiResponse> {
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
Expand Down
23 changes: 23 additions & 0 deletions src/utils/auth.rs
Original file line number Diff line number Diff line change
@@ -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<Client>,
email: String,
) -> Result<QueryOutput> {
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)
}
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit e4cbf7b

Please sign in to comment.