From cd149de34eddbe6ea8d24755d90d65968d000e70 Mon Sep 17 00:00:00 2001 From: Rajdeep Sengupta Date: Sun, 12 May 2024 13:22:32 +0530 Subject: [PATCH] login attempt reset --- src/core/auth.rs | 6 ++++++ src/core/user.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/core/auth.rs b/src/core/auth.rs index 7305a16..194685a 100644 --- a/src/core/auth.rs +++ b/src/core/auth.rs @@ -116,6 +116,12 @@ impl Auth { Err(e) => return Err(e), }; + // make the failed login attempts to 0 + match User::reset_failed_login_attempt(&mongo_client, &user.email).await { + Ok(_) => {} + Err(e) => return Err(e), + } + let res = SignInOrSignUpResponse { message: "Signin successful".to_string(), uid: user.uid, diff --git a/src/core/user.rs b/src/core/user.rs index acfcf8a..36d85e4 100644 --- a/src/core/user.rs +++ b/src/core/user.rs @@ -432,6 +432,55 @@ impl User { } } + pub async fn reset_failed_login_attempt( + mongo_client: &Client, + email: &str, + ) -> Result { + let db = mongo_client.database("test"); + let collection: Collection = db.collection("users"); + let dek_data = match Dek::get(&mongo_client, email).await { + Ok(dek) => dek, + Err(e) => { + return Err(e); + } + }; + + // find the user in the users collection using the uid + match collection + .update_one( + doc! { + "uid": Encryption::encrypt_data(&dek_data.uid, &dek_data.dek), + }, + doc! { + "$set": { + "failed_login_attempts": 0, + "updated_at": DateTime::now(), + } + }, + None, + ) + .await + { + Ok(cursor) => { + let modified_count = cursor.modified_count; + + // Return Error if User is not there + if modified_count == 0 { + // send back a 404 to + return Err(Error::UserNotFound { + message: "User not found".to_string(), + }); + } + return Ok("Failed login attempts reset".to_string()); + } + Err(_) => { + return Err(Error::ServerError { + message: "Failed to update User".to_string(), + }) + } + } + } + pub async fn change_password(mongo_client: &Client, email: &str, old_password: &str, new_password: &str) -> Result { let db = mongo_client.database("test"); let collection: Collection = db.collection("users");