From a85c74e7bd07e14797972708f6d3067ddb480ece Mon Sep 17 00:00:00 2001 From: Rajdeep Sengupta Date: Wed, 24 Jul 2024 13:48:11 +0530 Subject: [PATCH] Forget password Server page added --- .vscode/settings.json | 1 + src/core/user.rs | 9 +++- src/handlers/password_handler.rs | 93 ++++++++++++++++++++++++++++++-- src/main.rs | 2 + src/routes/auth_routes.rs | 3 +- src/routes/password_routes.rs | 3 +- src/routes/session_routes.rs | 5 +- src/routes/user_routes.rs | 7 +-- 8 files changed, 108 insertions(+), 15 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f9c6132..b766505 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "./Cargo.toml", ], "cSpell.words": [ + "blazingly", "bson", "chrono", "deks", diff --git a/src/core/user.rs b/src/core/user.rs index d880521..2379efb 100644 --- a/src/core/user.rs +++ b/src/core/user.rs @@ -685,7 +685,7 @@ impl User { &user.name, &user.email, &"Reset Password", - &format!("Please click on the link to reset your password: http://localhost:8080/forget-password-reset/{}", new_doc.id), + &format!("Please click on the link to reset your password: http://localhost:8080/forget-reset/{}", new_doc.id), ).send().await; Ok("Forget password request sent to email successfully".to_string()) @@ -702,6 +702,10 @@ impl User { let forget_password_requests_collection: Collection = db.collection("forget_password_requests"); + println!("Forget Password Request ID {:?}", req_id); + println!("Forget Password Request Email {:?}", email); + println!("Forget Password Request New Password {:?}", new_password); + // find the dek with the email let dek_data = match Dek::get(&mongo_client, &email).await { Ok(dek) => dek, @@ -717,6 +721,8 @@ impl User { .unwrap() .unwrap(); + println!("Forget Password Request {:?}", forget_password_request); + if forget_password_request.is_used { return Err(Error::ResetPasswordLinkExpired { message: "The link has already been used. Please request a new link.".to_string(), @@ -789,6 +795,7 @@ impl User { ).send().await; Ok("Password updated successfully".to_string()) + } pub async fn delete(mongo_client: &Client, email: &str) -> Result { diff --git a/src/handlers/password_handler.rs b/src/handlers/password_handler.rs index 3a05dbc..e9e0546 100644 --- a/src/handlers/password_handler.rs +++ b/src/handlers/password_handler.rs @@ -8,8 +8,7 @@ use crate::{ AppState, }; use axum::{ - extract::{Path, State}, - Json, + extract::{Path, State}, response::{Html, IntoResponse}, Json }; use axum_macros::debug_handler; use bson::doc; @@ -83,7 +82,7 @@ pub async fn forget_password_reset_handler( // check if payload is valid if payload.email.is_empty() | payload.password.is_empty() { return Err(Error::InvalidPayload { - message: "Email is required.".to_string(), + message: "Invalid Payload".to_string(), }); } match User::forget_password_reset(&state.mongo_client, &id, &payload.email, &payload.password) @@ -97,3 +96,91 @@ pub async fn forget_password_reset_handler( Err(e) => return Err(e), } } + +#[debug_handler] //forget_password_form +pub async fn forget_password_form(Path(id): Path) -> impl IntoResponse { + Html(format!(r#" + + + + + + Reset Password + + + + +

Reset Password

+
+
+ + + + + + +
+ +

Note: Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one number, and one special character.

+
+ +
+ + + + "#, id = id, api_key = dotenv::var("X_API_KEY").unwrap())) +} diff --git a/src/main.rs b/src/main.rs index ec89968..2df8e2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use axum::response::Html; use axum::routing::get; use axum::{middleware, Router}; use dotenv::dotenv; +use handlers::password_handler::forget_password_form; use middlewares::res_log::main_response_mapper; use middlewares::with_api_key::with_api_key; use mongodb::Client; @@ -43,6 +44,7 @@ async fn main() -> Result<(), Box> { // Define routes where middleware is not applied let public_routes = Router::new() .route("/", get(root_handler)) + .route("/forget-reset/:id", get(forget_password_form)) .merge(routes::health_check_routes::routes()) .layer(middleware::map_response(main_response_mapper)); diff --git a/src/routes/auth_routes.rs b/src/routes/auth_routes.rs index 02e5fb3..714b796 100644 --- a/src/routes/auth_routes.rs +++ b/src/routes/auth_routes.rs @@ -1,8 +1,7 @@ use axum::{extract::State, routing::post, Router}; use crate::{ - handlers::auth_handler::{signin_handler, signout_handler, signup_handler}, - AppState, + handlers::auth_handler::{signin_handler, signout_handler, signup_handler}, AppState }; pub fn routes(State(state): State) -> Router { diff --git a/src/routes/password_routes.rs b/src/routes/password_routes.rs index a0b6d3d..b6008d6 100644 --- a/src/routes/password_routes.rs +++ b/src/routes/password_routes.rs @@ -1,6 +1,6 @@ use axum::{extract::State, routing::post, Router}; -use crate::{handlers::password_handler::{forget_password_reset_handler, forget_password_request_handler, reset_password_handler}, AppState}; +use crate::{handlers::password_handler::{forget_password_request_handler, forget_password_reset_handler, reset_password_handler}, AppState}; pub fn routes(State(state): State) -> Router { let password_rotes = Router::new() @@ -8,5 +8,6 @@ pub fn routes(State(state): State) -> Router { .route("/forget-request", post(forget_password_request_handler)) .route("/forget-reset/:id", post(forget_password_reset_handler)); + Router::new().nest("/password", password_rotes).with_state(state) } \ No newline at end of file diff --git a/src/routes/session_routes.rs b/src/routes/session_routes.rs index e5c479b..c5f2af2 100644 --- a/src/routes/session_routes.rs +++ b/src/routes/session_routes.rs @@ -2,9 +2,8 @@ use axum::{extract::State, routing::{get, post}, Router}; use crate::{ handlers::session_handler::{ - delete_handler, delete_all_handler, get_all_from_uid_handler, get_details_handler, refresh_session_handler, revoke_handler, revoke_all_handler, verify_session_handler, get_all_handler - }, - AppState, + delete_all_handler, delete_handler, get_all_from_uid_handler, get_all_handler, get_details_handler, refresh_session_handler, revoke_all_handler, revoke_handler, verify_session_handler + }, AppState }; pub fn routes(State(state): State) -> Router { diff --git a/src/routes/user_routes.rs b/src/routes/user_routes.rs index 4316d81..2b59f0c 100644 --- a/src/routes/user_routes.rs +++ b/src/routes/user_routes.rs @@ -1,14 +1,11 @@ use axum::{ - extract::State, - routing::{get, post}, - Router, + extract::State, routing::{get, post}, Router }; use crate::{ handlers::user_handler::{ delete_user_handler, get_all_users_handler, get_recent_users_handler, get_user_email_handler, get_user_id_handler, toggle_user_activation_status, update_user_handler, update_user_role_handler - }, - AppState, + }, AppState }; pub fn routes(State(state): State) -> Router {