Skip to content

Commit

Permalink
login attempt reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajdip019 committed May 12, 2024
1 parent 967d67e commit cd149de
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions src/core/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,55 @@ impl User {
}
}

pub async fn reset_failed_login_attempt(
mongo_client: &Client,
email: &str,
) -> Result<String> {
let db = mongo_client.database("test");
let collection: Collection<User> = 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<String> {
let db = mongo_client.database("test");
let collection: Collection<User> = db.collection("users");
Expand Down

0 comments on commit cd149de

Please sign in to comment.